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

时间:2010-05-31 10:31:30

标签: wpf binding wpfdatagrid

我的产品类名为 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 datagrid。此数据网格显示已在集合中的项目(在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>

有些代码可以提供帮助

感谢

2 个答案:

答案 0 :(得分:5)

您的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));
        }
    }
}

这是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>

在Window的构造函数中的这一行

base.DataContext = new Produts();

答案 1 :(得分:1)

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