当绑定集合更新时,Wpf 数据网格行不更新?

发布于 2024-09-03 13:04:49 字数 5063 浏览 4 评论 0原文

我有一个名为 -Products.cs 的产品类,

class Products : INotifyPropertyChanged
{
    private int productId = 0;
    private int quantity = 0;
    private string description = string.Empty;
    private decimal price = 0.0m;

    public event PropertyChangedEventHandler PropertyChanged;

    public Products()
    {
    }

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

    #region Properties

    public int ProductID
    {
        get { return this.productId; }
        set
        {
            if (value != productId)
            {
                this.productId = value;
                OnPropertyChanged("ProductID");
            }
        }
    }

    public int Quantity
    {
        get { return this.quantity; }
        set
        {
            if (value != this.quantity)
            {
                this.quantity = value;
                OnPropertyChanged("Quantity");
                OnPropertyChanged("SubTotal");
            }

        }
    }

    public String Description
    {
        get { return this.description;}
        set
        {
            if (value != this.description)
            {
                this.description = value;
                OnPropertyChanged("Description");
            }
        }
    }

    public Decimal Price
    {
        get { return this.price; }
        set
        {
            if (value != this.price)
            {
                this.price = value;
                OnPropertyChanged("Price");
            }
        }
    }

    public Decimal SubTotal
    {
        get { return Quantity * Price; }
    }

    public static List<Products> ProductList
    {
        get { return new List<Products>(); }
    }     

    #endregion
}

然后我有 BilClass.cs 来实现添加、编辑和创建。从列表中删除项目。

class BillClass
{
    public List<Products> ProductsList = new List<Products>();
    Products products;

    public BillClass()
    {
        AddProducts(1, 2, 20.00m, "a");            
    }

    public void AddProducts(int _pid, int _quantity, decimal _price, string _desc)
    {
        products = new Products();
        products.ProductID = _pid;
        products.Quantity = _quantity;
        products.Price = _price;            
        products.Description = _desc;

        ProductsList.Add(products);
    }

    public bool RemoveProduct(int _id)
    {
        ProductsList.Remove(ProductsList.Find(e => e.ProductID.Equals(_id)));
        return true;
    }

    public void EditProducts(int _pid, int _quantity)
    {
        Products ob = ProductsList.Find(e => e.ProductID.Equals(_pid));
        ob.Quantity = _quantity;            
    }

    public List<Products> GetItems()
    {
        return ProductsList;
    }

我已将此“ProductList”绑定到 wpf 数据网格。此数据网格显示集合中已有的项目(在 BillClass 构造函数中添加的数据),但不显示通过包含 DataGrid 的销售窗口添加的新项目。

DataGrid XAML 代码

<Custom:DataGrid ItemsSource="{Binding Path=ProductsList}" Margin="16,100,16,120" AutoGenerateColumns="False" x:Name="dataGrid" HorizontalGridLinesBrush="#FFD0D0D0" 
            VerticalGridLinesBrush="#FFD0D0D0" CanUserDeleteRows="True" CanUserResizeRows="False" Background="White" IsTabStop="False" Focusable="False" IsReadOnly="True" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" GridLinesVisibility="Horizontal" EnableRowVirtualization="False">
            <Custom:DataGrid.Columns>
                <Custom:DataGridTextColumn Binding="{Binding Path=ProductID}" Header="Sl" Width="40" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=ProductID}" Header="Product Code" Width="100" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" Width="250" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=Price}" Header="Unit Price (in Rs.)" Width="120" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=Quantity}" Header="Quantity" Width="120" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=SubTotal, Mode=OneWay}" Header="Total (in Rs.)" Width="120" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />                                         
            </Custom:DataGrid.Columns>                      
        </Custom:DataGrid>

一些代码会有所帮助,

谢谢

I have product class named-Products.cs

class Products : INotifyPropertyChanged
{
    private int productId = 0;
    private int quantity = 0;
    private string description = string.Empty;
    private decimal price = 0.0m;

    public event PropertyChangedEventHandler PropertyChanged;

    public Products()
    {
    }

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

    #region Properties

    public int ProductID
    {
        get { return this.productId; }
        set
        {
            if (value != productId)
            {
                this.productId = value;
                OnPropertyChanged("ProductID");
            }
        }
    }

    public int Quantity
    {
        get { return this.quantity; }
        set
        {
            if (value != this.quantity)
            {
                this.quantity = value;
                OnPropertyChanged("Quantity");
                OnPropertyChanged("SubTotal");
            }

        }
    }

    public String Description
    {
        get { return this.description;}
        set
        {
            if (value != this.description)
            {
                this.description = value;
                OnPropertyChanged("Description");
            }
        }
    }

    public Decimal Price
    {
        get { return this.price; }
        set
        {
            if (value != this.price)
            {
                this.price = value;
                OnPropertyChanged("Price");
            }
        }
    }

    public Decimal SubTotal
    {
        get { return Quantity * Price; }
    }

    public static List<Products> ProductList
    {
        get { return new List<Products>(); }
    }     

    #endregion
}

Then I have BilClass.cs to implement add, edit & remove items from List.

class BillClass
{
    public List<Products> ProductsList = new List<Products>();
    Products products;

    public BillClass()
    {
        AddProducts(1, 2, 20.00m, "a");            
    }

    public void AddProducts(int _pid, int _quantity, decimal _price, string _desc)
    {
        products = new Products();
        products.ProductID = _pid;
        products.Quantity = _quantity;
        products.Price = _price;            
        products.Description = _desc;

        ProductsList.Add(products);
    }

    public bool RemoveProduct(int _id)
    {
        ProductsList.Remove(ProductsList.Find(e => e.ProductID.Equals(_id)));
        return true;
    }

    public void EditProducts(int _pid, int _quantity)
    {
        Products ob = ProductsList.Find(e => e.ProductID.Equals(_pid));
        ob.Quantity = _quantity;            
    }

    public List<Products> GetItems()
    {
        return ProductsList;
    }

I have binded this "ProductList" to wpf datagrid. This datagrid shows item already in collection (data added in BillClass constructor) but not showing new item added via sales window which contains DataGrid.

DataGrid XAML Code

<Custom:DataGrid ItemsSource="{Binding Path=ProductsList}" Margin="16,100,16,120" AutoGenerateColumns="False" x:Name="dataGrid" HorizontalGridLinesBrush="#FFD0D0D0" 
            VerticalGridLinesBrush="#FFD0D0D0" CanUserDeleteRows="True" CanUserResizeRows="False" Background="White" IsTabStop="False" Focusable="False" IsReadOnly="True" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" GridLinesVisibility="Horizontal" EnableRowVirtualization="False">
            <Custom:DataGrid.Columns>
                <Custom:DataGridTextColumn Binding="{Binding Path=ProductID}" Header="Sl" Width="40" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=ProductID}" Header="Product Code" Width="100" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" Width="250" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=Price}" Header="Unit Price (in Rs.)" Width="120" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=Quantity}" Header="Quantity" Width="120" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />
                <Custom:DataGridTextColumn Binding="{Binding Path=SubTotal, Mode=OneWay}" Header="Total (in Rs.)" Width="120" IsReadOnly="True" CanUserSort="False" CanUserResize="False" CanUserReorder="False" />                                         
            </Custom:DataGrid.Columns>                      
        </Custom:DataGrid>

Some code will help

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滴情不沾 2024-09-10 13:04:50

您的 Product 类必须实现 INotifyPropertyChanged 接口,并且您应该更新可绑定属性以在每次更改时引发此事件。这就是 WPF 中绑定的工作方式。
像这样的代码片段

public class Produts : INotifyPropertyChanged
    {
public Produts()
        {

        }
    int productID;
    public int ProductID
    {
        get { return productID; }
        set
        {
            if (productID != value)
            {
                productID = value;
                OnPropertyChange("ProductID");
            }
        }
    }
    int quantity;
    public int Quantity
    {
        get { return quantity; }
        set
        {
            quantity = value;
            OnPropertyChange("Quantity");
            //Force Subtotal to be updated
            OnPropertyChange("SubTotal");
        }
    }
    string description;
    public string Description
    {
        get { return description; }

        set
        {
            description = value;
            OnPropertyChange("Description");
        }
    }
    decimal price;
    public decimal Price
    {
        get { return price; }
        set
        {
            price = value;
            OnPropertyChange("Price");
            //Force Subtotal to be updated
            OnPropertyChange("SubTotal");
        }
    }
    public decimal SubTotal
    {
        get { return Quantity * Price; }
    }

    public static List<Produts> ProductList
    {
        get
        {
            return new List<Produts>();
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Here's XAML:

<DataGrid ItemsSource="{Binding Path=ProductList}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding Path= ProductID}"/>
                <DataGridTextColumn Header="Quantity" Binding="{Binding Path=Quantity}"/>
                <DataGridTextColumn Header="Description" Binding="{Binding Path=Description}"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}"/>
                <DataGridTextColumn Header="Subtotal" Binding="{Binding Path=SubTotal, Mode=OneWay}"/>
            </DataGrid.Columns>
        </DataGrid>

And this line in Window's constructor

base.DataContext = new Produts();

Your Product class has to implement INotifyPropertyChanged interface and you should update your bind-able properties to raise this event every time have changed. This is the way binding works in WPF.
Something like this snippet

public class Produts : INotifyPropertyChanged
    {
public Produts()
        {

        }
    int productID;
    public int ProductID
    {
        get { return productID; }
        set
        {
            if (productID != value)
            {
                productID = value;
                OnPropertyChange("ProductID");
            }
        }
    }
    int quantity;
    public int Quantity
    {
        get { return quantity; }
        set
        {
            quantity = value;
            OnPropertyChange("Quantity");
            //Force Subtotal to be updated
            OnPropertyChange("SubTotal");
        }
    }
    string description;
    public string Description
    {
        get { return description; }

        set
        {
            description = value;
            OnPropertyChange("Description");
        }
    }
    decimal price;
    public decimal Price
    {
        get { return price; }
        set
        {
            price = value;
            OnPropertyChange("Price");
            //Force Subtotal to be updated
            OnPropertyChange("SubTotal");
        }
    }
    public decimal SubTotal
    {
        get { return Quantity * Price; }
    }

    public static List<Produts> ProductList
    {
        get
        {
            return new List<Produts>();
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Here's XAML:

<DataGrid ItemsSource="{Binding Path=ProductList}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding Path= ProductID}"/>
                <DataGridTextColumn Header="Quantity" Binding="{Binding Path=Quantity}"/>
                <DataGridTextColumn Header="Description" Binding="{Binding Path=Description}"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}"/>
                <DataGridTextColumn Header="Subtotal" Binding="{Binding Path=SubTotal, Mode=OneWay}"/>
            </DataGrid.Columns>
        </DataGrid>

And this line in Window's constructor

base.DataContext = new Produts();
森末i 2024-09-10 13:04:50

它与 List 或 ObservableCollection 无关。集合所做的只是通知集合已更改(例如添加新项目、删除项目等)。单个项目本身应该通知 UI 某些字段已更改。您可以使用依赖属性或实现 INotifyPropertyChanged 来利用 WPF 属性系统。

It has nothing to do with List or ObservableCollection. All the collection does is to notify the collection has been changed (eg. add a new item, remove a item, etc). It's the individual item itself that should notify UI that some of the fields have been changed. You can either use dependency property or implement INotifyPropertyChanged to utilize WPF property system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文