目前我有一个用户集合。我使用该集合使用Checkboxes的datatemplate填充我的ItemsControl。我使用全选
填充第一个项目Analysts.Add(new UserDTO
{
Id = 0,
Name = "Select All",
IsSelected = true
});
我想知道如何创建事件,以便勾选任何复选框时会触发事件。我尝试设置Analysts.CollectionChanged += Analysts_CollectionChanged;
,但除非字面集改变,否则不会真正触发,而不是项目属性。
UsersDTO.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>
答案 0 :(得分:1)
你可能想要使用这样的东西:
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;
}
现在您可以订阅PropertyChanged
- 集合中元素的事件