我正在学习WPF,所以如果它是非常新手,请提出我的问题!我有一个Items集合,我想将该集合绑定到Grid并将Sum绑定到文本框。在线搜索我发现这个类即使我们改变了集合对象的属性也会引发事件。
ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)
但我似乎无法在我的代码中使用它。
这是我的代码
SaleItem
public class SaleItem : INotifyPropertyChanged
{
public int Num { get; set; }
public string ItemID { get; set; }
public string Name { get; set; }
private decimal price;
public decimal Price
{
get { return price; }
set
{
this.price = value;
OnPropertyChanged("Total");
}
}
public int quantity;
public int Quantity
{
get { return quantity; }
set
{
this.quantity = value;
OnPropertyChanged("Total");
}
}
public decimal Total
{
get { return decimal.Round(Price * Quantity, 2, MidpointRounding.AwayFromZero);}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
促销
public class Sale : INotifyPropertyChanged
{
private Decimal _total;
public TrulyObservableCollection<SaleItem> Items { get; set; }
public Sale()
{
Items = new TrulyObservableCollection<SaleItem>();
Items.Add(new SaleItem { ItemID = "1", Name = "Product 1", Price = 10, Quantity = 1 });
Items.Add(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });
Items.Add(new SaleItem { ItemID = "3", Name = "Product 3", Price = 10, Quantity = 1 });
}
public Decimal Total
{
get
{
return Items.Sum(x => x.Total);
}
set
{
_total = value;
OnPropertyChanged("Total");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
主窗口
public partial class MainWindow : Window
{
public Sale Model { get; set; }
public MainWindow()
{
InitializeComponent();
Model = new Sale();
this.DataContext = Model;
}
private void btnQuantity_Click(object sender, RoutedEventArgs e)
{
Model.Items.Add(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });
}
}
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:Sale />
</Window.DataContext>
<Grid>
<StackPanel>
<DataGrid x:Name="grdItems" ItemsSource="{Binding Items}"></DataGrid>
<TextBlock x:Name="txtTotal" Text="{Binding Total}"/>
<Button x:Name="btnQuantity" Content="Update" Click="btnQuantity_Click"/>
</StackPanel>
</Grid>
当我点击按钮并更新网格中的项目数量时,我正在尝试测试添加项目。如果我设置了一个断点并看到Total的值,那么它是正确的但不知何故它没有在UI上更新。我在这里缺少什么?
答案 0 :(得分:7)
你想做什么。当集合本身被更改时,是调用OnPropertyChanged
。重新计算总数。目前,Sale
类不知道集合已更新,并且需要更新其Total
属性。
您可以这样做的一种方法是检查NotifyCollectionChangedEvent
,如下所示:
在Sale()的构造函数中:
public Sale()
{
//Instantiate your collection and add items
Items.CollectionChanged += CollectionChanged;
}
然后添加事件的处理程序:
public void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged("Total");
}
如果除了更新Total属性之外,当集合发生变化时,你永远不会做任何其他事情。您可以通过删除事件处理程序并使用lambda表达式来进一步缩短代码:
Items.CollectionChanged += (s,e) => OnPropertyChanged("Total");
除非您计划在Total
课程中明确更新Sale
。可以删除setter
:
public Decimal Total
{
get
{
return Items.Sum(x => x.Total);
}
}
答案 1 :(得分:1)
在Sales.cs中添加:
public void AddItem(SaleItem item)
{
Items.Add(item);
OnPropertyChanged("Total");
}
在MainWindow.xaml.cs中调用:
Model.AddItem(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });
而不是
Model.Items.Add(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });
这将更新总计。