DataGridComboBoxColumn not updating ItemsSource
本文关键字:ItemsSource updating not DataGridComboBoxColumn | 更新日期: 2023-09-27 17:57:29
我有这个列:
<DataGridComboBoxColumn Header="Master" SelectedValueBinding="{Binding MasterId}" SelectedValuePath="Id" DisplayMemberPath="Name" ItemsSource="{Binding Data.Masters, Source={StaticResource proxy}}" />
ViewModel上的属性为:
public ObservableCollection<ReaderViewModel> Masters { get; set; }
DataGrid DataSource有一个自关系主从,我在任何Insert/update/Delete上更新集合,但ComboBox保留初始值,而不更新自己。
我做错了什么?
对于属性更改,我使用了Fody插件。如果你需要更多的代码来理解这个问题,我准备分享更多。
编辑1:
BindingProxy类:
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get
{
return GetValue(DataProperty);
}
set
{
SetValue(DataProperty, value);
}
}
public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
这个类可以在谷歌上找到,我就是在那里找到的
编辑2:
<DataGrid.Resources>
<classes:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>
BindingProxy的使用方式。
试试这个:
<DataGridComboBoxColumn Header="Master" SelectedValueBinding="{Binding MasterId}" SelectedValuePath="Id" DisplayMemberPath="Name" ItemsSource="{Binding Data.Masters, Source={StaticResource proxy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}}" />
更新:
private ObservableCollection<ReaderViewModel> masters;
public ObservableCollection<ReaderViewModel> Masters
{
get
{
return masters;
}
set
{
masters = value;
OnPropertyChanged("Masters");
}
}
protected void OnPropertyChanged(String propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
XAML:
<DataGridComboBoxColumn Header="Master" Binding="{Binding Name}" SelectedValueBinding="{Binding MasterId}" SelectedValuePath="Id" ItemsSource="{Binding Masters, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}"/>
</DataGridComboBoxColumn.Resources>
</DataGridComboBoxColumn>