简体   繁体   中英

DataGridComboBoxColumn not updating ItemsSource

I have this column:

<DataGridComboBoxColumn Header="Master" SelectedValueBinding="{Binding MasterId}" SelectedValuePath="Id" DisplayMemberPath="Name" ItemsSource="{Binding Data.Masters, Source={StaticResource proxy}}" />

The property on the ViewModel is:

public ObservableCollection<ReaderViewModel> Masters { get; set; }

The DataGrid DataSource has a self-relationship Master-Slaves and I update the collection on any Insert/Update/Delete but the ComboBox keeps the initial values and not updates itself.

What am I doing wrong?

For the property changing I'm using the Fody addon. If you need more code to understand the problem I'm ready to share more.

EDIT 1:

The BindingProxy class:

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));
}

This class can be found on Google, where I found it.

EDIT 2:

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

How the BindingProxy is used.

Try this:

<DataGridComboBoxColumn Header="Master" SelectedValueBinding="{Binding MasterId}" SelectedValuePath="Id" DisplayMemberPath="Name" ItemsSource="{Binding Data.Masters, Source={StaticResource proxy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}}" />

UPDATE:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM