我从数据库获取对象到 List,然后我将这个 List 绑定到 Datagrid。当我从数据库中获取数据时,无论如何都会触发 OnPropertyChanged(公共属性的设置器包含 OnPropertyChanged)!在 XAML(数据绑定)中,我使用这个公共属性。我究竟做错了什么?如何避免在数据导入期间触发事件?
我有课:
public class DescriptionOffProduct: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
private string description;
public string Description
{
get {return description;}
set{description = value; OnPropertyChanged(new PropertyChangedEventArgs("Description"));}
}
public DescriptionOffProduct(string description)
{
Description = description;
}
}
XAML:
<DataGrid Grid.Row="1" x:Name="DescriptionsGrid" AutoGenerateColumns="False" Margin="10" Grid.ColumnSpan="2" >
<DataGrid.Columns>
....
<DataGridTextColumn Width="*" Header="Описание" Binding="{Binding Path=Description, Mode=TwoWay}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我想看到这种行为:当我在应用程序的其他地方更改 Description 属性时,数据网格会更新单元格中的数据。