6

我有两个类,一个用于ViewModel,一个用于Product。Product 类有一个名为Line Total的属性,而 ViewModel 类有一个名为Total Amount的属性。Product 类绑定到 DataGrid 并且用户插入随后自动更新Line Total的数量。

这是ViewModel类:

public class ViewModel : INotifyPropertyChanged
{

    public ObservableCollection<Product> products { get; set; }// the children

    private decimal _TotalAmount; 
    public decimal TotalAmount // <=== has to hold sum of [products.LineTotal]
    {
        get
        {
            return totalAmount;
        }
        set
        {
            if (value != _TotalAmount)
            {
                _TotalAmount = value;
                onPropertyChanged(this, "TotalAmount");
            }
        }
    }

这是一个孩子的Product类:

public class Product : INotifyPropertyChanged
    {
        private decimal _LineTotal;
        public decimal LineTotal
        {
            get
            {
                return _LineTotal;
            }
            set
            {
                if (value != _LineTotal)
                {
                    _LineTotal = value;
                    onPropertyChanged(this, "LineTotal");
                }

            }

        }
}

我的问题是:TotalAmount如何计算所有 Products [Line Total]的总和?子Products如何通知父ViewModel更新TotalAmount

就像是:

foreach(var product in Products)
{
     TotalAmount += product.LineTotal;
}
4

4 回答 4

9

实现此目的的一种方法是,每次用户编辑行总计以及每次从ObservableCollection.

由于在设置新行总数时Product实现INotifyPropertyChanged并引发事件,因此可以处理该事件并重新计算总数。PropertyChangedViewModel

ObservableCollection有一个CollectionChanged在添加或删除项目时引发的事件,因此ViewModel也可以处理该事件并重新计算。(如果产品只能由用户更改而不是添加/删除等,则此部分不是真正必要的)。

你可以试试这个小程序,看看它是如何完成的:

代码隐藏

public partial class MainWindow : Window
{
    ViewModel vm = new ViewModel();

    public MainWindow()
    {
        InitializeComponent();

        vm.Products = new ObservableCollection<Product>
        {
            new Product { Name = "Product1", LineTotal = 10 },
            new Product { Name = "Product2", LineTotal = 20 },
            new Product { Name = "Product3", LineTotal = 15 }
        };

        this.DataContext = vm;
    }

    private void AddItem(object sender, RoutedEventArgs e)
    {
        vm.Products.Add(new Product { Name = "Added product", LineTotal = 50 });
    }

    private void RemoveItem(object sender, RoutedEventArgs e)
    {
        vm.Products.RemoveAt(0);
    }
}

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Product> _products;
    public ObservableCollection<Product> Products
    {
        get { return _products; }
        set
        {
            _products = value;

            // We need to know when the ObservableCollection has changed.
            // On added products: hook up eventhandlers to their PropertyChanged events.
            // On removed products: recalculate the total.
            _products.CollectionChanged += (sender, e) =>
            {
                if (e.NewItems != null)
                    AttachProductChangedEventHandler(e.NewItems.Cast<Product>());
                else if (e.OldItems != null)
                    CalculateTotalAmount();
            };

            AttachProductChangedEventHandler(_products);
        }
    }

    private void AttachProductChangedEventHandler(IEnumerable<Product> products)
    {
        // Attach eventhandler for each products PropertyChanged event.
        // When the LineTotal property has changed, recalculate the total.
        foreach (var p in products)
        {
            p.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == "LineTotal")
                    CalculateTotalAmount();
            };
        }

        CalculateTotalAmount();
    }

    public void CalculateTotalAmount()
    {
        // Set TotalAmount property to the sum of all line totals.
        TotalAmount = Products.Sum(p => p.LineTotal);
    }

    private decimal _TotalAmount;
    public decimal TotalAmount
    {
        get { return _TotalAmount; }
        set
        {
            if (value != _TotalAmount)
            {
                _TotalAmount = value;

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("TotalAmount"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class Product : INotifyPropertyChanged
{
    public string Name { get; set; }

    private decimal _LineTotal;
    public decimal LineTotal
    {
        get { return _LineTotal; }
        set
        {
            if (value != _LineTotal)
            {
                _LineTotal = value;

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("LineTotal"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <DataGrid ItemsSource="{Binding Products}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" />
                <DataGridTextColumn Binding="{Binding LineTotal}" />
            </DataGrid.Columns>
        </DataGrid>

        <Button Click="AddItem">Add item</Button>
        <Button Click="RemoveItem">Remove item</Button>

        <TextBlock>
            <Run>Total amount:</Run>
            <Run Text="{Binding TotalAmount}" />
        </TextBlock>
    </StackPanel>
</Window>
于 2013-01-31T22:00:41.933 回答
2

如果ParentViewModel关心何时ChildModel更新属性,它应该订阅它的PropertyChanged事件。

但是,由于您有一个 Collection ,因此应该在事件中添加/删除连接事件ChildModels的处理程序。PropertyChangedCollectionChanged

// Hook up CollectionChanged event in Constructor
public MyViewModel()
{
    Products = new ObservableCollection<Product>();
    MyItemsSource.CollectionChanged += Products_CollectionChanged;
}

// Add/Remove PropertyChanged event to Product item when the collection changes
void Products_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
    if (e.NewItems != null)
        foreach(Product item in e.NewItems)
            item.PropertyChanged += Product_PropertyChanged;

    if (e.OldItems != null)
        foreach(Product item in e.OldItems)
            item.PropertyChanged -= Product_PropertyChanged;
}

// When LineTotal property of Product changes, re-calculate Total
void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "LineTotal")
    {
        TotalAmount = products.Sum(p => p.LineTotal);

        // Or if calculation is in the get method of the TotalAmount property
        //onPropertyChanged(this, "TotalAmount");
    }
}
于 2013-02-01T13:56:11.770 回答
1

我相信 bernd_rausch 的答案是正确的。基本问题是为什么要将 TotalAmount 存储在 ViewModel 中?唯一的原因可能是您拥有的产品太多会影响性能。但即使在这种情况下,您也必须小心保持值一致。

最安全的方法是编写一个 TotalAmount 属性来即时计算 TotalAmount。然后链接 Changed 事件。

public class ViewModel : INotifyPropertyChanged
{
  ViewModel()
  {
    Products = new ObservableCollection<Product>();
    Products.CollectionChanged += OnProductsChanged;
  }
  public ObservableCollection<Product> Products { get; private set; }// the children
  public decimal TotalAmount { get { return Products.Select(p => p.LineTotal).Sum(); } }

  private void OnProductChanged(object sender, PropertyChangedEventArgs eventArgs)
  {
     if("LineTotal" != eventArgs.PropertyName)
           return;
     onPropertyChanged(this, "TotalAmount");
  }

  private void OnProductsChanged(object sender, NotifyCollectionChangeEventArgs eventArgs)
  {
     // This ignores a collection Reset...
     // Process old items first, for move cases...
     if (eventArgs.OldItems != null)
       foreach(Product item in eventArgs.OldItems)
         item.PropertyChanged -= OnProductChanged;

     if (eventArgs.NewItems != null)
       foreach(Product item in eventArgs.NewItems)
        item.PropertyChanged += OnProductChanged;


     onPropertyChanged(this, "TotalAmount");
  }

}

我忽略了重置案例。但我认为这应该给你正确的方向。如果您想缓存计算结果,我仍然会使用此方法并通过在其中一个更改处理程序中重置的内部惰性值进行缓存。

于 2013-02-01T14:29:35.680 回答
0

我认为 UI 中 TotalAmount 的值仅在您设置 TotalAmount 以触发 NotifyPropertyChanged 事件时才会更新。为此,您必须监听所有产品的 PropertyChangedEvent,并且当集合更改或产品的 LineTotal 更改时,您必须将 TotalAmount 设置为与 _TotalAmount 不同的某个值。

但是这段代码真的很难理解:不清楚为什么要存储一个值,即每次读取(TotalAmount)时都会计算一个变量(_TotalAmount)。并且由于 _TotalAmount 未设置为零,因此不是正确的值。

于 2012-10-02T14:28:03.083 回答