在ViewModel中更改数据后,绑定到实体框架的WPF DataGrid不会更新 [英] WPF DataGrid bound to entity framework doesn't update after change of data in ViewModel
问题描述
我正在开发WPF应用程序,其中我已将数据网格绑定到实体框架。
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>
ItemsSource绑定到视图模型中的一个属性,该属性是我的实体Formula_field_attributes的列表。
该实体包含attribute_name和assigned_field_name列。
这是viewmodel属性:
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");
}
}
开头的列表仅包含属性名,assigned_field_name为空白。
然后我可以为列表中的属性分配一个字段名。
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");
当我在操作后查看viewmodel属性时,看起来不错。
但是我看不到我在数据网格中所做的更改。
我怎么了?
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?
谢谢!
推荐答案
同时我通过以下步骤解决了这个问题:
Meanwhile i resolved the problem by the following steps:
- 我将viewmodel属性的类型更改为ObeservableCollection。
- 我对设置方法进行了更改。我创建了一个新的ObservableCollection对象,从虚拟机属性中获取项目,设置所需的assigned_field_name,最后,我用新对象设置了虚拟机属性。
我知道这不是我应该做的,但是可以。
I know it's not what i should do, but it works.
这篇关于在ViewModel中更改数据后,绑定到实体框架的WPF DataGrid不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!