我试图从几个角度来解决这个问题。在这里我试图将它简化成一个小测试用例。WPF DataGrid单元格绑定到属性属性的域对象不更新
我有一个DataGrid单元格更新绑定到属性属性的问题。该属性由另一列中的绑定组合框单元格设置。绑定的对象是一个如下,我指的是物业:
public class MainObject : INotifyPropertyChanged
{
private int _subObjectId;
public virtual SubObject SubObjectObj { get; set; }
public int SubObjectId {
get { return _subObjectId; }
set { _subObjectId = value; SubObjectObj = <GetObjFromDB> };
}
...
}
public class SubObject : INotifyPropertyChanged
{
public int Id { get; set; }
public string Name { get; set; }
public string Specialty{ get; set; }
...
}
数据网格的ItemsSource是
public ObservableCollection<MainObject> SourceData;
现在,DataGrid中列的子对象的选择组合框。在它旁边的TextBox列是(假定)显示在ComboBox中选择的任何SubObject的SubObject.Specialty。
<DataGridTemplateColumn Header="SubObjects">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SubObject.Name, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBoxSubObject" ItemsSource="{Binding Model.SubObjects, RelativeSource={RelativeSource AncestorType={x:Type uch:TestControl}}}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValue="{Binding SubObjectId, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="ComboBoxDoctor_OnSelectionChanged"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Specialty" Binding="{Binding Path=SubObjectObj.Specialty}"/>
当电网最初画,专业列是正确的 - 它的性质是什么子对象显示在另一列。但是当我更改组合框时,专业栏不会更改。无论如何告诉DataGrid专业列绑定源已更改并刷新?
感谢您的任何建议。
是的 - 这就是我所缺少的是对专业的明确OnPropertyChanges。谢谢!而SubObjectId的原因是外键在实体框架代码优先POCO中是如何建模的。 – cardinalPilot 2013-04-29 15:34:59
啊,好的。乐意效劳 :) – ChrisO 2013-04-29 17:09:58