使用MVVM的Process Modeller

时间:2012-06-07 06:56:31

标签: wpf mvvm process designer

我正处于开发WPF应用程序(使用MVVM模式)的早期阶段,该应用程序将允许用户为进程创建流程图。

初步视图如下所示:enter image description here

左侧窗格中的符号是WPF路径对象。

需要做的是用户必须能够将符号从符号面板拖到图表部分上。

现在,在具有代码隐藏事件的直接WPF中,这一切都非常简单,但我需要有关如何使用MVVM模式实现此功能的建议。我假设在我的模型中我将有一个Observable集合,其中包含拖动到画布上的所有符号(?)。每次将符号拖到画布上时,我该如何更新该集合?

我理解,理想情况下,在使用MVVM时,视图的代码隐藏必须完全为空,但如果我将代码放在那里专门处理视图上的事件,它会破坏模式吗?

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

在画布'ViewModel中,定义属性

public ObservableCollection<SymbolViewModel> Symbols { get; }

并在您的视图中使用ItemsControl来显示符号:

<ItemsControl ItemsSource="{Binding Symbols}" ... />

当然,您需要在ItemsControl中定义适当的数据模板,项目模板和项目面板。

ObservableCollection实现了INotifyCollectionChanged,确保ItemsControl自动更新。

答案 1 :(得分:1)

如果您询问 ANY 事件如何适合MVVM,那么您必须记住MVVM是关于从ViewViewModel Model抽象View {1}}不知道ViewModel是什么以及底层Model是什么。

所以坚持这个想法,有2个观点。

1. If a view based event is handled in the code behind of the view (such as in `MyView.xaml.cs`) then as long as it does not refer the model or the viewmodel, it is perfectly fine in MVVM. Because such event handler is strictly a `View` specific code and should remain confined in the View layer (XAML or XAML.CS).

2. If an event handler has to refer the model or the viewmodel then it should not be handled in the code behind of the view but should be converted into `Command` based behavior using Attached Properties.

对于你的拖拽场景我宁愿没有2.谷歌的附加属性和MVVM w.r.t.活动,你会发现很好的例子。