如何通过WPF DataGrid中的更改来更新ObservableCollection项的属性? [英] How do I update an ObservableCollection item's property from change in a WPF DataGrid?
问题描述
我有一个WPF DataGrid,其数据源是一个ObservableCollection.它的设置大致如下:
I have a WPF DataGrid who's data source is an ObservableCollection. It is set up loosely as the following:
public class ItemDataCollection : ObservableCollection<ItemData>
{
}
public class ItemData : INotifyPropertyChanged
{
private bool _selected = true;
public bool Selected
{
get
{
return _selected;
}
set
{
if (value != _selected)
{
_selected = value;
NotifyPropertyChanged("Selected");
}
}
}
}
_itemDataCol = new ItemDataCollection();
<... fill the _itemDataCol with data here ...>
dataGrid1.ItemsSource = _itemDataCol;
更新集合时,dataGrid1.Items.Refresh()会很好地更新dataGrid1.但是,当我通过选中或取消选中与该属性相对应的行中的复选框来修改一行的"Selected"属性时,集合中的项目不会得到更新.我调查了ObeservableCollection的CollectionChanged事件,但似乎没有被触发.我需要什么布线来获取dataGrid1以更新集合.
When the collection is updated, a dataGrid1.Items.Refresh() updates dataGrid1 nicely. However when I modify the "Selected" property of a row by checking or unchecking the checkbox in the row corresponding to that property, the item in the collection does not get updated. I looked into the CollectionChanged event of the ObeservableCollection, but that is not appearing to get triggerd. What wiring do I need to get the dataGrid1 to update the collection.
更新
我所做的只是将ItemSource属性设置为ObservableCollection并让列自动生成.从那以后,我更改为直接绑定,并找到了解决该问题的更多详细信息.当我简单地选中复选框时,不会触发任何通知.但是,如果我在选中复选框后点击,则集合将更新.这是绑定:
All I was doing was setting the ItemSource property to the ObservableCollection and letting the columns auto-generate. I have since changed to bind directly, and found more detail to the problem. When I simply check the box - no notification is triggerd. However if I hit after checking the box, then the collection is updated. Here is the binding:
<DataGridCheckBoxColumn Binding="{Binding Path=Selected, Mode=TwoWay}" Header="Selected"></DataGridCheckBoxColumn>
所以我想问题是,如何在选中或取消选中该框后必须点击才能获得更新?
So I guess the question is how do I get the update with having to hit after checking or unchecking the box?
更新#2 (我的代表还不够高,所以我无法回答)好的-我想我有解决方案.如果在绑定中包含"UpdateSourceTrigger = PropertyChanged",则一切似乎正常.
Update #2 (I cannot answer as my rep is not high enough yet) OK - I think I have the solution. If I include "UpdateSourceTrigger=PropertyChanged" in the binding everything seems to work.
<DataGridCheckBoxColumn Binding="{Binding Path=Selected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Selected"></DataGridCheckBoxColumn>
如果对此有任何负面影响,请留下评论.感谢您的帮助!
Please leave comments if there are any negative affects of this that I may be missing. Thanks for the help!
推荐答案
CollectionChanged用于插入和删除.NotifyPropertyChanged用于更新项目.在发布的代码中,您实际上并没有实现INotifyPropertyChanged.
CollectionChanged is for Insert and Delete. NotifyPropertyChanged is for update of an items. In the posted code you don't actually implement INotifyPropertyChanged.
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
我认为绑定到返回_itemDataCol的公共属性会更清洁
And I think it is cleaner to bind to a public property where you return _itemDataCol
否则,celopez3的TwoWay答案
Otherwise the TwoWay answer of celopez3
这篇关于如何通过WPF DataGrid中的更改来更新ObservableCollection项的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!