Wpf 使用MVVM的流程建模器

Wpf 使用MVVM的流程建模器,wpf,mvvm,process,designer,Wpf,Mvvm,Process,Designer,我正处于开发WPF应用程序(使用MVVM模式)的早期阶段,该应用程序将允许用户创建流程图 初步意见如下: 左侧窗格中的符号是WPF路径对象 需要做的是,用户必须能够将符号从符号面板拖到图表部分 现在,在带有代码隐藏事件的直接WPF中,这一切都非常简单,但我需要关于如何使用MVVM模式实现这一点的建议。我假设在我的模型中,我将有一个可观察的集合,其中包含拖到画布上的所有符号(?)。每次将符号拖到画布上时,如何更新该集合 我理解,理想情况下,使用MVVM时视图的代码必须完全为空,但是如果我将专门处理

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

初步意见如下:

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

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

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

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


任何帮助都将不胜感激。

在画布的ViewModel中,定义一个属性

public ObservableCollection<SymbolViewModel> Symbols { get; }
当然,您需要在
ItemsControl
中定义适当的数据模板、项目模板和项目面板


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

如果您询问任何事件如何适合MVVM,那么您必须记住MVVM是关于从
视图模型和
模型中抽象
视图。
View
不知道它的
ViewModel
是什么以及底层的
模型是什么

因此,坚持这个观点,有两种观点

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号。谷歌的附加属性和MVVMW.r.t.事件,你会发现很好的例子

你给了我一些建议,谢谢。关于如何将“符号”面板中的拖动功能实现到图表画布上的任何建议?请使用WPF的DragDrop.DoDragDrop()启动拖动操作,并在画布的Drop事件处理程序中,使用DragEventArgs.GetPosition()确定符号应放置的鼠标位置。
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.