MVVM 和控件创建
想象一个简单的场景,其中包含一个包含按钮和一些空白空间的 WPF 窗口。单击该按钮将创建一个新的自定义/用户控件并将其随机放置在窗口上的某个位置。
单击这些控件之一会将其从窗口中删除。
现在我有一个 ViewModel ala MVVM,它公开了一个用于“创建新”按钮的 ICommand,但是创建新控件的代码在哪里?我猜每个控件可能都有自己的 ViewModel,它将处理其删除和定位。
可以在窗口上没有代码隐藏并且 ViewModel 对视图没有真正了解的情况下实现吗?
Imagine a simple scenario with a WPF window containing a button and some clear space. Clicking the button creates a new custom/user control and places it somewhere randomly on the window.
Clicking one of these controls will remove it from the window.
So now I have a ViewModel ala MVVM which exposes an ICommand for the "create new" button, but where does the code to create the new control live? Each control will probably have its own ViewModel which will handle its deletion and positioning I guess.
Can it be achieved with no code behind on the window AND no real knowledge of the View by the ViewModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
导致创建控件的代码位于“主”ViewModel 中。
实际创建控件的代码是容器。
所以它会像这样:
更新:在写答案时,我忘记了并非所有 MVVM 框架都像 Prism 那样具有区域。所以请原谅上面代码的特殊性,因为它并没有真正改变任何东西。您只需要自己构建类似 Region 抽象的东西。让我们看看:
MyView
然后会将事件处理程序附加到该事件,并从ChildViewModelAddedEventArgs
内部获取ChildView
实例,以便可以添加它到ItemsControl
,它是父级,而您的 ViewModel 不会弄乱这些细节。当然,这意味着您现在需要一些代码隐藏,但除非您使用本身提供此类服务的框架,否则这是没有帮助的。
The code that causes the controls to be created lives inside your "main" ViewModel.
The code that actually creates the controls is the container.
So it would go something like:
Update: When writing the answer, I forgot that not all MVVM frameworks have Regions as Prism does. So excuse the specificity of the code above, as it doesn't really change anything. You simply need to build something like the Region abstraction yourself. Let's see:
MyView
would then attach an event handler to this event, and pick up theChildView
instance from insideChildViewModelAddedEventArgs
so that it can be added to anItemsControl
it is the parent of without your ViewModel messing with such details.Of course this means that you now need some code-behind, but this cannot be helped unless you are using a framework that provides such services itself.
这应该可以通过在 ItemsControl 上进行一些非常仔细的数据绑定来实现,不确定如何实现布局,但是您将有一个包含子视图模型集合的父视图模型,然后由 ItemsControl 执行布局。当父 ViewModel 创建子 ViewModel 时,它应该注入 RelayCommand 作为 lambda 表达式,以从父集合中删除并清理子 ViewModel。
This SHOULD be doable with some very careful databinding on an ItemsControl, not sure how you would achieve the layout, but you will have a parent view model containing a collection of child view models, layout would then be preformed by the ItemsControl. When the parent ViewModel created the child ViewModel, it should inject a RelayCommand as a lambda expression to remove and cleanup the child ViewModel from the parents collection.