简体   繁体   中英

WPF DataGrid bound to entity framework doesn't update after change of data in ViewModel

I'm working on a WPF-application in which i have bound a datagrid to entity framework.

This is my datagrid definition:

            <DataGrid x:Name="grd_formula_arguments" Grid.Row="1" Grid.Column="2" Style="{StaticResource commonControlStyle}" IsReadOnly="True"
                      ItemsSource="{Binding FormulaArgumentsAndFields, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                      AutoGenerateColumns="False" EnableRowVirtualization="True"
                      CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="col_attribute_name" Binding="{Binding attribute_name}" Header="Argument"/>
                    <DataGridTextColumn x:Name="col_assigned_field_name" Binding="{Binding assigned_field_name}" Header="Feld"/>
                </DataGrid.Columns>
            </DataGrid>

The ItemsSource is bound to a property in the viewmodel, which is a list of my entity formula_field_attributes. That entity contains the columns attribute_name and assigned_field_name. This is the viewmodel property:

public List<formula_field_attributes> FormulaArgumentsAndFields
{
    get { return formulaArgumentsAndFields; }
    set
    {
        formulaArgumentsAndFields = value;
        RaisePropertyChanged("FormulaArgumentsAndFields");
    }
}

At the beginning the list only contains attributenames and assigned_field_name is blank. Then i can assign a fieldname to an attribute in the list.

        var argumentname = ((formula_field_attributes)grd_formula_arguments.SelectedItem).attribute_name;
        foreach (var item in changeTextProperties.FormulaArgumentsAndFields)
        {
            if (item.attribute_name == argumentname)
            {
                item.assigned_field_name = fieldName;
            }
        }
        changeTextProperties.RaisePropertyChanged("FormulaArgumentsAndFields");

When i take a look to the viewmodel property after my operations, it looks fine. But i can't see my changes in the datagrid. What did i wrong?

Thanks in advance!

Meanwhile i resolved the problem by the following steps:

  1. I changed the type of the viewmodel property to ObeservableCollection.
  2. I made a change in the set-method. I create a new ObservableCollection-object, get the items from the VM-property, set the assigned_field_name i want and - finally i set the VM-property with the new object.

I know it's not what i should do, but it works.

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