作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习WPF所以如果它是新手请提出我的问题!我有一个项目集合,我想将该集合绑定(bind)到一个网格并将总和绑定(bind)到一个文本框。在线搜索我发现即使我们更改集合对象的属性,这个类也会引发事件。
ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)
但我似乎无法让它在我的代码中工作。
这是我的代码
促销品
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 上更新。我在这里缺少什么?
最佳答案
你想做什么。是在集合本身发生更改时调用 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");
除非您计划自己在 Sale
类中显式更新 Total
。 setter
可以被删除:
public Decimal Total
{
get
{
return Items.Sum(x => x.Total);
}
}
关于c# - 如何在 WPF 中绑定(bind) Observable 集合的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985342/
我是一名优秀的程序员,十分优秀!