2010-05-31 183 views
3

我有產品類named- Products.cs綁定集合更新時Wpf datagrid行不更新?

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> 

一些代碼將有助於

感謝

+0

你是什麼意思「但沒有表現出通過其中包含的DataGrid銷售窗口添加新項目」的意思。您是否從另一個窗口填充產品列表網格?如果是這樣,你是怎麼做到的? – 2010-06-01 08:10:55

回答

5

您的產品類必須實現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> 

而此行的窗口的構造

base.DataContext = new Produts(); 
+0

我面臨同樣的問題。正如你所說,我已經實現了代碼。請幫助... 公共事件PropertyChangedEventHandler PropertyChanged; (PropertyChanged!= null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } public int ProductID { get {return productId; } 集合{ 如果(的productId =值!) { 的productId =值; OnPropertyChanged(「ProductID」); } } } – 2010-05-31 14:50:38

+0

雖然WPF綁定系統不支持您的代碼,但您的實現並未反映出您的想法,即您在更新數量項目時必須更新的問題。 我更新的代碼,其中包括XAML和代碼背後反映此事,現在就試試...... – 2010-05-31 17:08:46

+0

先生阿布杜拉它沒有爲我工作,所以我編輯了我的文章,並提供所有代碼在這個綁定工作。希望得到一些肯定的答案。我試過你在代碼中提到的代碼,但沒有成功,之後我試圖操縱事物。所以不要生氣。 我知道我可以將項目添加到List <>但無法觸發事件來更新DataGrid。 感謝您的幫助 – 2010-06-01 07:00:44

1

它無關,與列表或的ObservableCollection。所有的收集工作都是通知集合已被更改(例如添加一個新項目,刪除一個項目等)。這是個別項目本身應該通知UI某些字段已被更改。您可以使用依賴屬性或實現INotifyPropertyChanged來利用WPF屬性系統。