由于可观察的集合,我将数据从DbSet绑定到DataGrid。 DbSet => DataGrid的方式运行良好,但DataGrid => DbSet的方式根本没有。
这是一些代码,可以向您展示我所做的事情:
首先,具有Binding属性的Datagrid:
<DataGrid x:Name="DonneesBrutes" ItemsSource="{Binding Path=.ResultatCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="PMRQ" Width="*" Binding="{Binding Path=.TOTMPMRQ, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="PMRQ"></DataGridTextColumn>
<DataGridTextColumn x:Name="LibellePMRQ" Width="*" Binding="{Binding Path=.LibelléTOTApres, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Libellé PMRQ"></DataGridTextColumn>
<DataGridTextColumn x:Name="Ligne" Width="*" Binding="{Binding Path=.Remarque, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Ligne"></DataGridTextColumn>
<DataGridTextColumn x:Name="OTM" Width="*" Binding="{Binding Path=.TOTMPMRQ, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="OTM"></DataGridTextColumn>
<DataGridTextColumn x:Name="TOTM" Width="*" Binding="{Binding Path=.SiModifie, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="TOTM"></DataGridTextColumn>
<DataGridTextColumn x:Name="LibelleTOTM" Width="*" Binding="{Binding Path=.LibelléTOTApres, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Libellé OTM"></DataGridTextColumn>
<DataGridTextColumn x:Name="GA" Width="*" Binding="{Binding Path=.Groupe_D_alerte, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="GA"></DataGridTextColumn>
<DataGridTextColumn x:Name="Discipline" Width="*" Binding="{Binding Path=.NomTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Discipline"></DataGridTextColumn>
<DataGridTextColumn x:Name="DisciplineSubstituee" Width="120" Binding="{Binding Path=.NomChamp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Discipline Substituée"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
这是ResultatCollection,它是我用来将数据从DbSet绑定到DataGrid的ObservableCollection:
class ViewModel:INotifyPropertyChanged
{
private BDDInterneEntities _BDDInterneEntities;
public ViewModel()
{
_BDDInterneEntities = new BDDInterneEntities();
ResultatCollection = new ObservableCollection<Resultat>(_BDDInterneEntities.Resultat);
}
public ObservableCollection<Resultat> ResultatCollection { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
然后这就是DbSet Resultat的样子:
public partial class Resultat:INotifyPropertyChanged
{
public string NomTable { get; set; }
public string Groupe_D_alerte { get; set; }
public string NomChamp { get; set; }
public string TOTMPMRQ { get; set; }
public string SiModifie { get; set; }
public string LibelléTOTAvant { get; set; }
public string LibelléTOTApres { get; set; }
public string Remarque { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
我听说过有关DataGrid.Items.refresh()但听起来不行,DataGrid.ItemsSource = null然后DataGrid.ItemsSource = myDataSource但它也没有。
我也听说过mode = TwoWay和UpdateSourceTrigger = PropertyChanges就足够了,我很遗憾。
提前感谢您的帮助。
问候。
答案 0 :(得分:2)
要接收绑定通知,您必须正确实施INotifyPropertyChanged
。你做它的方式使它可以编译,但没有其他。
每次外部可见属性发生变化时,您都必须提升PropertyChanged
事件。这就是为什么你通常会看到像
private int myProperty;
public int MyProperty
{
get { return myProperty; }
set
{
if (myProperty != value)
{
myProperty = value;
OnPropertyChanged("MyProperty");
}
}
}
当然,如果要通知该类的用户有关属性更改的信息,则不得直接写入该字段,而是使用该属性。
正如您所看到的,这变得非常繁琐,这就是为什么有几个MVVM框架提供简化版本,但您也可以自己编写它的版本,例如
protected void Set<T>(ref T field, T newValue, string propertyName)
{
if (field != newValue)
{
field = newValue;
OnPropertyChanged(propertyName);
}
}
然后你的财产将成为
public int MyProperty
{
get { return myProperty; }
set { Set(ref myProperty, value, "MyProperty"); }
}