在WPF和MVVM模式下基于嵌套模型实体构建ViewModel [英] Building ViewModels based on nested Model Entities in WPF and MVVM Pattern

查看:241
本文介绍了在WPF和MVVM模式下基于嵌套模型实体构建ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何基于以下模型构建视图模型时遇到问题

I have a problem understanding how to build view models based on the following models

(为了简化起见,我简化了模型)

(I simplified the models to be clearer)

public class Hit
{
   public bool On { get; set;}
   public Track Track { get; set; }
}
public class Track
{
   public ObservableCollection<Hit> Hits { get; set; }
   public LinearGradientBrush Color { get; set; }
   public Pattern Pattern { get; set; }
}
public class Pattern
{
   public string Name { get; set; }
   public ObservableCollection<Tracks> Tracks { get; set; }
}

现在,我的问题是,如何构建ViewModels.

Now, my problem is, how to build the ViewModels..

我需要通过模型保持原始关系,因为我在Pattern上有一个Serialize()方法,可将其序列化为XML文件.(以及相关的Tracks和Hits)

I need to keep the original relationships through the models, beacaus i have a Serialize() method on the Pattern that serializes it to an XML file.. (with the related Tracks and Hits)

为了能够将模式绑定到用户控件及其嵌套模板,我还应该拥有一个带有ObservableCollection< TrackViewModel>的PatternViewModel.在其中,对于TrackViewModel和HitViewModel ..同样,我希望在不属于业务对象(颜色和其他内容)的视图模型上具有自定义表示属性.

To be able to bind the pattern to the user controls and it's nested templates I should also have a PatternViewModel with an ObservableCollection<TrackViewModel> in it, same thing for the TrackViewModel and the HitViewModel.. and i neet to have custom presentation properties on the view models that aren't part of the business object (colors and more..)

在视图模型上复制模型的所有关系对我来说似乎不是一件好事... 并且在编码视图模型时跟踪所有这些关系也更容易出错.

It just seem not a good thing to me to duplicate all of the relationships of the models on the view models... and keeping track of all this relations while coding the viewmodels is also much more error prone..

有人有更好的方法/解决方案吗?

anyone has a better approach/solution?

推荐答案

我最终使用了乔·怀特(Joe White)提出的部分解决方案,其方式略有不同

I ended up using part of the solution that Joe White suggested, in a slighty differ manner

解决方案是只保留模型的开始,然后将内部集合的CollectionChanged的事件处理程序附加到集合上,例如,PatternViewModel将为:

The solution was to just leave the models as they were at the beginning, and attaching to the collections an eventhandler for CollectionChanged of the inner collections, for example, the PatternViewModel would be:

public class PatternViewModel : ISerializable
{
    public Pattern Pattern { get; set; }
    public ObservableCollection<TrackViewModel> Tracks { get; set; }

    public PatternViewModel(string name)
    {
        Pattern = new Pattern(name);
        Tracks = new ObservableCollection<TrackViewModel>();
        Pattern.Tracks.CollectionChanged += new NotifyCollectionChangedEventHandler(Tracks_CollectionChanged);
    }

    void Tracks_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                foreach (Track track in e.NewItems)
                {
                    var position = Pattern.Tracks.IndexOf((Track) e.NewItems[0]);
                    Tracks.Insert(position,new TrackViewModel(track, this));
                }
                break;
            case NotifyCollectionChangedAction.Remove:
                foreach (Track track in e.OldItems)
                    Tracks.Remove(Tracks.First(t => t.Track == track));
                break;
            case NotifyCollectionChangedAction.Move:
                for (int k = 0; k < e.NewItems.Count; k++)
                {
                    var oldPosition = Tracks.IndexOf(Tracks.First(t => t.Track == e.OldItems[k]));
                    var newPosition = Pattern.Tracks.IndexOf((Track) e.NewItems[k]);
                    Tracks.Move(oldPosition, newPosition);
                }
                break;
        }
    }
}

因此,我可以在视图模型上附加新的颜色/样式/命令,以保持基本模型的清洁

So i can attach the new Color/Style/Command on the view models to keep my base models clean

每当我在基础模型集合中添加/删除/移动项目时,视图模型集合便会保持彼此同步

And whenever I add/remove/move items in the base models collection, the view models collections remain in sync with each other

幸运的是,我不必在应用程序中管理很多对象,因此重复的数据和性能不会成为问题

Luckily I don't have to manage lots of object in my application, so duplicated data and performance won't be a problem

我不太喜欢它,但是它运作良好,并且工作量也不大,只是一个视图模型的事件处理程序,其中包含其他视图模型集合(在我的情况下,是一个用于使PatternViewModel同步的集合) TrackViewModels,另一个在TrackViewModel上用于管理HitViewModels)

I don't like it too much, but it works well, and it's not a huge amount of work, just an event handler for the view model that contains others view model collections (in my case, one for PatternViewModel to sync TrackViewModels and another on TrackViewModel to manage HitViewModels)

仍然对您的想法或更好的想法感兴趣=)

Still interested in your thoughs or better ideas =)

这篇关于在WPF和MVVM模式下基于嵌套模型实体构建ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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