C# DataGrid绑定ObservaleCollection时未重新刷新INotifyPropertyChanged

C# DataGrid绑定ObservaleCollection时未重新刷新INotifyPropertyChanged,c#,wpf,binding,datagrid,inotifypropertychanged,C#,Wpf,Binding,Datagrid,Inotifypropertychanged,我正在将列表绑定到数据网格,并将其属性绑定到数据网格文本列,我为我的ListProduct实现了INotifyPropertyChanged 当我更改产品的任何属性时,它将在我的DataGrid中更新,但当添加或删除任何产品时,它将不会在DataGrid中更新 我的数据网格 <DataGrid x:Name="ProductList" AutoGenerateColumns="False" IsReadOnly="True"> <DataGrid.Col

我正在将
列表
绑定到
数据网格
,并将其属性绑定到
数据网格文本列
,我为我的ListProduct实现了
INotifyPropertyChanged

当我更改产品的任何属性时,它将在我的DataGrid中更新,但当添加或删除任何产品时,它将不会在DataGrid中更新

我的数据网格

    <DataGrid x:Name="ProductList" AutoGenerateColumns="False" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
            <DataGridTextColumn Header="Code" Binding="{Binding Code}"/>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Value" Binding="{Binding Value, StringFormat=R\{0:C\}}"/>
        </DataGrid.Columns>
    </DataGrid>

还有我的代码

public partial class PageProduct : Page, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<Pastel> _ListProduct;
    public ObservableCollection<Pastel> ListProduct
    {
        get { return _ListProduct; }
        set
        {
            _ListProduct = value;
            this.OnPropertyChanged("ListProduct");
        }
    }

    public PagePastel()
    {

        InitializeComponent();

        UpdateList();
        ProductList.ItemsSource = ListProduct;   // ProductList is my DataGrid
    }

    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }

    private void UpdateList()
    {
        // db is my EntityContext
        ListProduct = new ObservableCollection<Product>(db.Products.ToList());
    }

    private void btDeletar_Click(object sender, RoutedEventArgs e)
    {
        if (ProductList.SelectedItem != null)
        {
            Product product = ProductList.SelectedItem as Product;

            db.Product.Remove(product);
            db.SaveChanges();
            if (SystemMessage.ConfirmDeleteProduct() == MessageBoxResult.No)
                return;

            SystemMessage.ProductDeleteSuccess();
            UpdateList();
        }
        else
            SystemMessage.NoProductSelected();
    }
公共部分类PageProduct:Page,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私人可观察收集(清单产品);;
公共可观测收集列表产品
{
获取{return\u ListProduct;}
设置
{
_ListProduct=价值;
本协议项下,OnPropertyChanged(“ListProduct”);
}
}
公共页粉彩()
{
初始化组件();
UpdateList();
ProductList.ItemsSource=ListProduct;//ProductList是我的数据网格
}
私有void OnPropertyChanged(字符串p)
{
if(PropertyChanged!=null)
{
房地产变更(这是新的房地产变更发展(p));
}
}
私有void UpdateList()
{
//db是我的EntityContext
ListProduct=新的ObservableCollection(db.Products.ToList());
}
私有无效btDeletar\U单击(对象发送者,路由目标e)
{
if(ProductList.SelectedItem!=null)
{
Product Product=ProductList。选择EdItem作为产品;
db.Product.Remove(产品);
db.SaveChanges();
if(SystemMessage.ConfirmDeleteProduct()==MessageBoxResult.No)
返回;
SystemMessage.ProductDeleteSuccess();
UpdateList();
}
其他的
SystemMessage.NoProductSelected();
}
问题在哪里?当我添加或删除任何寄存器时,我可以为DataGrid更新列表做什么



解决方案:

问题是您正在覆盖ListProduct字段引用的ObservableCollection,而没有更改ProductList.ItemsSource。这导致ListProduct属性将指向新列表,而ProductList.ItemsSource仍将指向原始列表

仅仅在ListProduct上引发PropertyChanged事件是行不通的,因为您没有对ItemsSource属性使用绑定。您有几个选项

1) 将UpdateList更改为:

private void UpdateList()
{
    // db is my EntityContext
    ListProduct = new ObservableCollection<Product>(db.Products.ToList());
    ProductList.ItemsSource = ListProduct;
}
或者更好的方法是,更改btDeletar\u单击以仅从ProductList中删除所选项目,如所示:

private void btDeletar_Click(object sender, RoutedEventArgs e)
{
    if (ProductList.SelectedItem != null)
    {
        Product product = ProductList.SelectedItem as Product;

        db.Product.Remove(product);
        db.SaveChanges();

        // *** Side note, should db.SaveChanges be after this if statement?? ***
        if (SystemMessage.ConfirmDeleteProduct() == MessageBoxResult.No)
            return;

        SystemMessage.ProductDeleteSuccess();
        // Don't call UpdateList just remove the item from the list. 
        // This will raise the CollectionChanged event and the grid will respond accordingly.
        ProductList.Remove(product);
    }
    else
        SystemMessage.NoProductSelected();
}

很抱歉,我尝试了此解决方案,但不起作用。以及为什么我的INotifyPropertyChanged不起作用?可能与的重复
private void btDeletar_Click(object sender, RoutedEventArgs e)
{
    if (ProductList.SelectedItem != null)
    {
        Product product = ProductList.SelectedItem as Product;

        db.Product.Remove(product);
        db.SaveChanges();

        // *** Side note, should db.SaveChanges be after this if statement?? ***
        if (SystemMessage.ConfirmDeleteProduct() == MessageBoxResult.No)
            return;

        SystemMessage.ProductDeleteSuccess();
        // Don't call UpdateList just remove the item from the list. 
        // This will raise the CollectionChanged event and the grid will respond accordingly.
        ProductList.Remove(product);
    }
    else
        SystemMessage.NoProductSelected();
}