我有一个像这样的ObservableCollection,
ObservableCollection<Item> Found_Items = new ObservableCollection<Item>();
public struct Item
{
public bool Enabled { get; set; }
public BitmapImage ItemIcon { get; set; }
public string Path { get; set; }
public string Size { get; set; }
}
我正在设置Datagrid的itemsource,
FoundItemsDatagrid.ItemsSource = Found_Items;
我在Datagrid中有一个像这样的复选框,
<DataGridTemplateColumn Header="Path" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我想,每当我选中或取消选中datagrid上的复选框时,它都应该更新我的ObservableCollection。
最简单的方法是什么?
谢谢..
答案 0 :(得分:3)
我按照说明HERE。
我将“Item”结构改为“Item”类,就像这样;
public class Item : INotifyPropertyChanged
{
private bool _Enabled;
private BitmapImage _ItemIcon;
private string _Path;
private string _Size;
public event PropertyChangedEventHandler PropertyChanged;
public Item(bool enabled, BitmapImage itemIcon, string path, string size)
{
_Enabled = enabled;
_ItemIcon = itemIcon;
_Path = path;
_Size = size;
}
public bool Enabled
{
get { return _Enabled; }
set
{
_Enabled = value;
this.NotifyPropertyChanged("Enabled");
}
}
public BitmapImage ItemIcon
{
get { return _ItemIcon; }
set
{
_ItemIcon = value;
this.NotifyPropertyChanged("ItemIcon");
}
}
public string Path
{
get { return _Path; }
set
{
_Path = value;
this.NotifyPropertyChanged("Path");
}
}
public string Size
{
get { return _Size; }
set
{
_Size = value;
this.NotifyPropertyChanged("Size");
}
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
现在每件事都很完美。
答案 1 :(得分:2)
问题是你如何绑定你的收藏。您正在显式设置ItemsSource,因此ObservableCollection将无法按您希望的方式工作。
而是像这样使用绑定:
<DataGridTemplateColumn Header="Path" Width="*" ItemsSource="{Binding Found_Items}" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
然后确保您在后台执行此操作:
public ObservableCollection<Item> Found_Items {get; set;}
要反映每个项目的更改,您需要像这样使用INotifyPropertyChanged:
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool enabled;
private BitmapImage itemIcon;
private string path;
private string size;
public string Size
{
get { return size; }
set
{
size = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Size"));
}
}
public string Path
{
get { return path; }
set
{
path = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Path"));
}
}
public BitmapImage ItemIcon
{
get { return itemIcon; }
set
{
itemIcon = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ItemIcon"));
}
}
public bool Enabled
{
get { return enabled; }
set
{
enabled = value;
if(PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs("Enabled"));
}
}
}
现在,当用户更改项目时,可以在ObservableCollection中看到更改。这要归功于INotifyPropertyChanged。