gpt4 book ai didi

c# - 通知集合中某项的属性更改

转载 作者:行者123 更新时间:2023-12-03 10:34:27 25 4
gpt4 key购买 nike

目前,我目前有一个用户集合。我使用集合用Checkboxes的数据模板填充ItemsControl。我用全选填充第一项

Analysts.Add(new UserDTO
{
Id = 0,
Name = "Select All",
IsSelected = true
});

我想知道如何创建事件,以便如果选中任何复选框,就会触发一个事件。我试图设置 Analysts.CollectionChanged += Analysts_CollectionChanged;,但是除非真正改变集合,而不是更改项目属性,否则不会真正触发。

用户DTO.cs
public int Id {get; set;}
private string _name;
public string Name
{
get { return _name; }
set {_name = value; OnPropertyChanged("Name");}
}

private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set { _isSelected = value; OnPropertyChanged("IsSelected"); }
}

DismissAppointmentView
<ItemsControl Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Analysts, UpdateSourceTrigger=PropertyChanged}" IsTabStop="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<telerik:RadUniformGrid Rows="2"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox telerik:StyleManager.Theme="Windows8" Margin="3 3 12 3" Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

最佳答案

您可能要使用以下内容:

 public class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged
{
public ObservableCollectionEx() : base() { }

public ObservableCollectionEx(List<T> list)
: base((list != null) ? new List<T>(list.Count) : list)
{
CopyFrom(list);
}

public ObservableCollectionEx(IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
CopyFrom(collection);
}

private void CopyFrom(IEnumerable<T> collection)
{
IList<T> items = Items;
if (collection != null && items != null)
{
using (IEnumerator<T> enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
items.Add(enumerator.Current);
}
}
}
}

protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
item.PropertyChanged += Item_PropertyChanged;
}

protected override void RemoveItem(int index)
{
Items[index].PropertyChanged -= Item_PropertyChanged;
base.RemoveItem(index);
}

protected virtual void MoveItem(int oldIndex, int newIndex)
{
T removedItem = this[oldIndex];
base.RemoveItem(oldIndex);
base.InsertItem(newIndex, removedItem);
}

protected override void ClearItems()
{
foreach (T item in Items)
{
item.PropertyChanged -= Item_PropertyChanged;
}
base.ClearItems();
}

protected override void SetItem(int index, T item)
{
T oldItem = Items[index];
T newItem = item;
oldItem.PropertyChanged -= Item_PropertyChanged;
newItem.PropertyChanged += Item_PropertyChanged;
base.SetItem(index, item);
}

private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var handler = ItemPropertyChanged;
if (handler != null) { handler(sender, e); }
}

public event PropertyChangedEventHandler ItemPropertyChanged;
}

现在,您可以订阅集合中Elements的 PropertyChanged -Event

关于c# - 通知集合中某项的属性更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com