在“主从视图"视图中汇总详细信息值 [英] Aggregate detail values in Master-Detail view

查看:68
本文介绍了在“主从视图"视图中汇总详细信息值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些自定义实体中具有主从关系.说我有以下结构:

I have a master-detail relationship in some custom entities. Say I have the following structure:

class Master : INotifyPropertyChanged
{
    public int Id { get; set; } // + property changed implementation 
    public string Name { get; set; } // + property changed implementation

    public ObservableCollection<Detail> Details { get; }
}

class Detail : INotifyPropertyChanged
{
     public int Id { get; private set; }
     public string Description { get; set; }
     public double Value { get; set; } // + property changed implementation
}

我的目标是使用GridView创建一个ListView,以显示Master对象的列表.当我选择一个特定的母版时,我将有一个单独的ListView来查看详细信息,从而可以进行编辑.基本上是一个相当标准的Master-Detail视图.

My goal is to have a ListView, using a GridView, showing a list of Master objects. When I select a specific Master, I'll have a separate ListView for the details, allowing editing. Basically, a fairly standard Master-Detail view.

但是,我还希望Master的GridView显示该主人的所有Detail元素的总和,即:Details.Select(d => d.Value).Sum();

However, I also want the GridView for the Master to show the Sum of all of that master's Detail elements, ie: Details.Select(d => d.Value).Sum();

使用自定义IValueConverter可以很容易地显示它.我可以将详细信息集合直接转换为双倍显示的总和,并通过IValueConverter将TextBlock的文本绑定到Details OneWay.这将起作用,并且在我打开窗口时显示正确的值.

This is fairly easy to display using a custom IValueConverter. I can convert from the details collection directly to a double displaying sum, and bind a TextBlock's Text to the Details OneWay, via the IValueConverter. This will work, and show the correct values when I open the window.

但是,如果我更改了一个detail成员,则不会更新(即使detail实现了INotifyPropertyChanged),因为集合本身仍然是相同的(ObservableCollection引用未更改).

However, if I change one of the detail members, this will not update (even though detail implements INotifyPropertyChanged), since the collection itself is still the same (the ObservableCollection reference hasn't changed).

我想在主列表中有一个汇总值,在明细列表中显示总和(或平均值/计数/等),并在用户更改明细属性时保持最新状态.我该如何实施呢?

I want to have an aggregated value in a master list, showing the sum (or average/count/etc) within the detail list, and have this stay up to date when the user changes properties in details. How can I go about implementing this?

理想情况下,我希望有一种方法可以实现这一目标,而又不涉及直接更改Master类.有问题的应用程序正在使用MVVM模式,我真的更愿意不更改Model类来实现特定的View.有没有办法在不向模型中引入自定义逻辑的情况下做到这一点?

Ideally, I would prefer if there is a means of accomplishing this that doesn't involve changing the Master class directly. The application in question is using the MVVM pattern, and I'd really prefer to not change my Model classes in order to implement a specific View. Is there a way to do this without introducing custom logic into the model?

推荐答案

我正在考虑使用UI的可能性,在该UI中您可以使绑定显式并通过命令执行绑定/更新...但是,这似乎是最简单的方法要做的就是扩展ObservableCollection以在每个Detail实例的添加/删除中添加/删除侦听器,然后在其中的任何一个更改时触发CollectionChanged.将其命名为 DeeplyObservableCollection< T>.

I was considering possibilities with the UI where you'd make the binding explicit and perform binding/updates from a command... but it seems that the easiest way to do it would be to extend the ObservableCollection to add/remove listeners to each Detail instance as its added/removed, then just fire CollectionChanged when any of them change. Call it DeeplyObservableCollection<T>.

class Master : INotifyPropertyChanged
{
    public int Id { get; set; } // + property changed implementation 
    public string Name { get; set; } // + property changed implementation
    public double Sum {get {return Details.Sum(x=>x.Value);}}

    public DeeplyObservableCollection<Detail> Details { get; }

    // hooked up in the constructor
    void OnDOCChanged(object sender, CollectionChangedEventArgs e) 
    { OnPropertyChanged("Sum"); }
}

如果无法正确覆盖所需的所有方法,则最糟糕的情况是您必须将ObservableCollection包装为另一种类型.

Worst case you'd have to wrap an ObservableCollection in another type if you can't properly override all the methods you need...

这篇关于在“主从视图"视图中汇总详细信息值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆