分配新值后将 ObservableCollection 绑定到 DataGrid
这似乎是一个简单的问题,但我无法让它发挥作用。
我有一个具有以下属性的 UserControl:
public ObservableCollection<HL7Message> source {get; set;}
和以下 Binding:
<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True"
ItemsSource="{Binding source}" ></data:DataGrid>
来自父 UserControl 我在单击按钮时设置了值:
messagesGrid.source = src; //messagesGrid is the name of the UserCntrol above
我期望我的 DataGrid 被更新,但事实并非如此。你能指出我做错了什么吗?
It seems to be a simple problem, but I can't get it to work.
I have a UserControl with the following property:
public ObservableCollection<HL7Message> source {get; set;}
And the following Binding:
<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True"
ItemsSource="{Binding source}" ></data:DataGrid>
from a parent UserControl I set the value upon a button click:
messagesGrid.source = src; //messagesGrid is the name of the UserCntrol above
I'm expecting my DataGrid to be updated, but it's not. Can you please point at what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自动属性遗憾的是不支持更改通知。因此,如果您设置
source
属性,DataGrid 将不会知道集合已更改。一种可能性是为
messagesGrid.source 实现 INotifiyPropertyChanged code>-Property:
请注意,我将
Source
的第一个字母写成大写,因为在.net中,属性通常是这样写的。您必须相应地更改绑定,因为绑定区分大小写。Auto-properties sadly do not support change-notification. Therefore the DataGrid will not know that the collection has been changed if you set the
source
-Property.One possibility is to implement INotifiyPropertyChanged for the
messagesGrid.source
-Property:Please note, I have written the first letter of
Source
in UpperCase because in .net, properties are generally written so. You have to change your binding accordingly because Bindings are case sensitive.问题是,当您单击按钮时
source
的引用发生变化时,没有任何信息可以告诉 UI 自行更新。您需要将source
设置为依赖属性,或者实现INotifyPropertyChanged
,并在source< 的 setter 中调用
PropertyChanged
事件。 /代码>。The problem is that when the reference for
source
changes on your button click, there is nothing to tell the UI to update itself. You will either need to makesource
a dependency property, or implementINotifyPropertyChanged
, and invoke thePropertyChanged
event in the setter forsource
.