简体   繁体   English

绑定ObservableCollection的DataGrid不能使用INotifyPropertyChanged重新刷新

[英]DataGrid Binding a ObservableCollection not reflash with INotifyPropertyChanged

I'm binding a List<Product> to a DataGrid and Binding it properties to DataGridTextColumns and I implemented INotifyPropertyChanged for my ListProduct 我绑定List<Product>DataGrid并绑定属性DataGridTextColumns和我实现INotifyPropertyChanged我ListProduct

When I change any property of Product, it will update in my DataGrid, but when a add or delete any Product, it will not update in DataGrid 当我更改Product的任何属性时,它将在我的DataGrid中更新,但是当添加或删除任何Product时,它不会在DataGrid中更新

My 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>

And my code behind 还有我的代码

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();
    }

Where is problem? 问题在哪里? What can I do for DataGrid update list when I add or delete any register? 添加或删除任何寄存器时如何处理DataGrid更新列表?


Solution: https://stackoverflow.com/a/8470177/2209497 解决方案: https : //stackoverflow.com/a/8470177/2209497

The problem is you're overwriting the ObservableCollection that the ListProduct field references and not changing the ProductList.ItemsSource. 问题是您正在覆盖ListProduct字段引用的ObservableCollection,而不更改ProductList.ItemsSource。 What that causes is that the ListProduct property will be pointed to a new list while ProductList.ItemsSource will still point to the original. 这是因为ListProduct属性将指向一个新列表,而ProductList.ItemsSource仍将指向原始列表。

Just raising the PropertyChanged event on ListProduct won't work beause you're not using a Binding for the ItemsSource property. 仅在ListProduct上引发PropertyChanged事件将无法正常工作,因为您没有对ItemsSource属性使用Binding。 You've got a couple of options. 您有两种选择。

1) Change UpdateList to: 1)将UpdateList更改为:

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

and

public PagePastel()
{

    InitializeComponent();

    UpdateList();
}

Or what would probably be better, change btDeletar_Click to just remove the selected item from ProductList as in: 或者可能会更好,更改btDeletar_Click以仅从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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定ObservableCollection是否需要INotifyPropertyChanged? - Is INotifyPropertyChanged needed for binding ObservableCollection? 将具有INotifyProperty的ObservableCollection更改为DatagridComboboxColumn绑定 - ObservableCollection with INotifyPropertyChanged into DatagridComboboxColumn Binding Datagrid绑定到ObservableCollection WPF - Datagrid Binding to ObservableCollection WPF 将ObservableCollection绑定到DataGrid ComboBox? - Binding ObservableCollection to DataGrid ComboBox? 将DataGrid绑定到ObservableCollection <Dictionary> - Binding DataGrid to ObservableCollection<Dictionary> 将ObservableCollection绑定到DataGrid - Binding an ObservableCollection to a DataGrid ListView,UserControl,DependencyProperty,ObservableCollection,INotifyPropertyChanged的绑定错误 - Binding error with ListView, UserControl, DependencyProperty, ObservableCollection, INotifyPropertyChanged 在没有INotifyPropertyChanged的情况下更新ObservableCollection成员属性的绑定 - Updating binding for ObservableCollection member property without INotifyPropertyChanged 将Datagrid绑定到ObservableCollection <T> 在SIlverlight - Binding Datagrid to ObservableCollection<T> in SIlverlight 绑定ObservableCollection <T> ItemSource到DataGrid - Binding ObservableCollection<T> ItemSource to DataGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM