DataGridComboBoxColumn不更新ItemsSource

时间:2016-07-18 07:48:10

标签: c# .net wpf xaml mvvm

我有这个专栏:

<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有一个自我关系的Master-Slaves,我在任何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));
}

这个课程可以在我找到的Google上找到。

编辑2:

<DataGrid.Resources>
    <classes:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>

如何使用BindingProxy。

1 个答案:

答案 0 :(得分:-1)

试试这个:

<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>