删除后面的UserControl代码以用于MVVM [英] Removing UserControl code behind for use with MVVM

查看:73
本文介绍了删除后面的UserControl代码以用于MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MVVM创建用户控件。
基本上,我试图包装一个组合框,该组合框将从存储库中提取数据。这将允许我在应用程序的许多不同视图中使用同一组合框。在整个应用程序中将有许多包装好的组合框。

I am trying to create a user control using MVVM. Basically I am trying to wrap a combobox that will pull data from a respository. This will allow me to use the same combobox in many different views in my application. There will be many of the wrapped comboboxes throughout the application.

我很容易就能使用DependencyProperty和背后的代码来创建此控件。现在,我试图将其转换为MVVM,并且在弄清楚如何将值返回到绑定到组合框所在的View的ViewModel中时遇到了麻烦。

I was easily able to create this control using a DependencyProperty and code-behind. I am now trying to convert this to MVVM and am having trouble figuring out how to get the value back to /from the ViewModel that in bound to the View where my combobox is located.

这时任何想法或建议都将不胜感激。

Any ideas or suggestions would be greatly appreciated at this point.

谢谢,
Eric

Thanks, Eric

推荐答案

使用MVVM时,使用后面带有代码的 UserControl 是完全可以接受的。如果您确实想将功能从 UserControl 中移出,则将其移至需要其父视图模型的任何地方。如果不想在多个地方重复相同的代码,则可以将其封装在一个类中,然后将该类的实例作为属性添加到每个相关视图模型中。

It is perfectly acceptable to use a UserControl that has code behind in it when using MVVM. If you really want to move the functionality out of the UserControl, then move it to whichever parent view models will require it. If you don't want to have the same code repeated in several places, you could encapsulate it in a class and add an instance of that class as a property to each of the relevant view models.

这篇关于删除后面的UserControl代码以用于MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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