这似乎是一个简单的问题,但我无法让它发挥作用。
我有一个具有以下属性的用户控件:
public ObservableCollection<HL7Message> source {get; set;}
以及以下绑定:
<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 能够更新,但事实并非如此。你能指出我做错了什么吗?
自动属性 http://msdn.microsoft.com/en-us/library/bb384054.aspx遗憾的是不支持更改通知。因此,如果您设置,DataGrid 将不知道集合已更改source
-财产。
一种可能性是实施INotifyPropertyChanged http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx为了messagesGrid.source
-财产:
class YourUserControlClass: INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
if (null != PropertyChanged) {
PropertyChanged(this,e);
}
}
ObservableCollection<HL7Message> m_source;
public ObservableCollection<HL7Message> Source { g
get{return m_source;}
set{
if(value != m_source){
m_source=value;
OnPropertyChanged("Source");
}
}
}
....
请注意,我已经写了第一封信Source
大写,因为在 .net 中,属性通常是这样写的。您必须相应地更改绑定,因为绑定区分大小写。
<data:DataGrid x:Name="dgMessages" Grid.Row="2" AutoGenerateColumns="True" ItemsSource="{Binding Source}" ></data:DataGrid>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)