wpf 如何实现INotifyPropertyChanged,如果我最初从数据库设置类的属性?

lvmkulzt  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(138)

如果我问了一些愚蠢的问题,很抱歉,但是我没有找到答案。我从数据库中获取DescriptionOffProduct对象到List,然后将此List绑定到Datagrid。
但是当我从数据库中获取数据时,OnPropertyChanged仍然会触发!我做错了什么?如何避免在数据导入过程中触发事件?我有一个类:

public class DescriptionOffProduct: INotifyPropertyChanged
{  
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    private int id;
    private int producttypeid;
    private string description;
    public int Id { get; set; }
    public int ProductTypeId { get; set; }
    public string Description
    {
        get {return description;}
        set{description = value; OnPropertyChanged(new PropertyChangedEventArgs("Description"));}
    }
    public DescriptionOffProduct(int id, int producttypeid, string description)
    {
        Id = id;
        ProductTypeId = producttypeid;
        Description = description;
    }
}

XAML:

<DataGrid Grid.Row="1" x:Name="DescriptionsGrid" AutoGenerateColumns="False" Margin="10" Grid.ColumnSpan="2" >
    <DataGrid.Columns>
        <DataGridTextColumn Width="*" Header="Id описания" Binding="{Binding Path=Id, StringFormat=#{0}, Mode=OneWay}" ></DataGridTextColumn>
        <DataGridTextColumn Width="*" Header="Id типа" Binding="{Binding Path=ProductTypeId, Mode=OneWay}"></DataGridTextColumn>
        <DataGridTextColumn Width="*" Header="Описание" Binding="{Binding Path=Description, Mode=TwoWay}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

我想看到这样的行为:当我在应用程序的其他地方更改Description属性时,datagrid会更新单元格中的数据。
下面是我如何绑定集合:

public void FillDescriptions()
            {
                Repository repository = new Repository();
                string sql = "select *from descriptions";
                DataTable table = repository.GetTableQuery(sql);
                var reader = table.CreateDataReader();
                using (reader)
                {
                    while (reader.Read())
                    {
                        descriptionsList.Add(new DescriptionOffProduct((int)reader["id"], (int)reader["producttypeid"], reader["description"].ToString()));
                    }
                }
                
            }

public List<DescriptionOffProduct> descriptionsList = new List<DescriptionOffProduct>();
        public List<DescriptionOffProduct> descrsOfSelected = new List<DescriptionOffProduct>();
private void TypesGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedProduct = (TypeOffProduct)TypesGrid.SelectedItem;
            int IdValue = selectedProduct.Id;
            descrsOfSelected =
                descriptionsList.Where(descr => descr.ProductTypeId == IdValue).ToList();
            DescriptionsGrid.ItemsSource = descrsOfSelected;
        }
j2datikz

j2datikz1#

如果我理解正确的话,您不希望在构造对象时引发PropertyChanged事件。
您可以更改构造函数以设置支持字段而不是属性:

public DescriptionOffProduct(int id, int producttypeid, string description)
{
    this.id = id;
    this.producttypeid = producttypeid;
    this.description = description;
}

这样,PropertyChanged事件仅在公共属性更新时引发。
您还应该确保PropertyChanged事件在属性的backing字段发生实际更改时引发,例如:

public string Description
{
    get => description;
    set
    {
        if(description.Equals(value)) return;
        description = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(Description));
    }
}

相关问题