C#Wpf编辑Datagrid不会更新它的itemsource [英] C# Wpf Editing Datagrid does not update it's itemsource
问题描述
我有一个像这样的ObservableCollection,
I have an ObservableCollection like this,
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,
I'm setting Datagrid's itemsource like this,
FoundItemsDatagrid.ItemsSource = Found_Items;
我在Datagrid中有一个这样的复选框,
I have a checkbox in Datagrid like this,
<DataGridTemplateColumn Header="Path" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我想要,每当我选中或取消选中datagrid上的复选框时,它都应该更新我的ObservableCollection.
I want, whenever i check or uncheck the checkbox on datagrid it should update my ObservableCollection.
最简单的方法是什么?
谢谢..
推荐答案
问题是您如何绑定到集合.您正在显式设置ItemsSource,因此ObservableCollection将无法按您希望的方式工作.
The problem is how you are binding to your collection. You are setting the ItemsSource explicitly therefore the ObservableCollection will not work the way you want it to.
而不是像这样使用绑定:
Instead use binding like so:
<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>
然后确保您在后台执行此操作:
Then ensure you do this in the background:
public ObservableCollection<Item> Found_Items {get; set;}
要反映每个项目的更改,您需要像这样使用INotifyPropertyChanged:
For the changes of each item to be reflected you need to use INotifyPropertyChanged like so:
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.
Now when an item is changed by the users the change can be seen in the ObservableCollection. This is thanks to that INotifyPropertyChanged.
这篇关于C#Wpf编辑Datagrid不会更新它的itemsource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!