Wpf 用户控件和 MVVM [英] Wpf UserControl and MVVM

查看:21
本文介绍了Wpf 用户控件和 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为我的应用程序编写 WPF 用户控件.我在我的应用程序中使用 MVVM.

I am thinking about writing a WPF User Control for my application. I am using MVVM in my application.

用户控件可能需要可以设置我的父视图的依赖属性.使用 MVVM 时,想法是父视图最终会在 UserControls DP 与父视图的 VM 之间创建绑定)

User control's may require Dependency Properties that can be set my Parent View. when using MVVM, the idea is that the Parent View will eventually create a binding between the UserControls DP with Parent View's VM)

Dependency Properties 需要在 View 类中创建,因为 VM 不继承自 DependencyObject.这意味着在后面的 XAML 代码中添加代码.

Dependency Properties need to be created in the View class as VM do not inherit from DependencyObject. This means adding code within the XAML code behind.

我想知道您是否可以就我在使用 MVVM 开发 WPF 应用程序时应该如何设计用户控件提供建议...

I was wondering if you can give advice as to how I should design a user control when developing WPF application using MVVM...

推荐答案

案例 1: 如果您创建此控件只是为了在您的应用程序中使用,那么您可以继续创建一个 ViewModel,但是你不需要创建 DP,你的 ViewModel 可以只实现 INotifyPropertyChanged 并且你的父 Vm 仍然可以绑定到它们.

Case 1: If you are creating this control just to be consumed in your application then you can go ahead and create a ViewModel for it, but then you don't need to create DP's, your ViewModel can just implement INotifyPropertyChanged and your parent Vm can still bind to them.

在我们的例子中,对于用户控件,我们创建了单独的 VM,并且它的一个实例存在于 ParentVM 中.因此,父视图将包含此控件并将 UserControlVM 绑定到此控件(ParentVM.UserControlVM),而 usercontrol 将负责其他绑定.

In our case, for user controls we have created separate VM's and an instance of it was present in ParentVM. So parent view will have this control in it and will bind the UserControlVM to this control(ParentVM.UserControlVM) and usercontrol will take care of other bindings.

案例 2:如果您的控件将被其他应用程序/开发人员使用,并且您不想保持简单,那么继续在控件模板实现之后创建自定义控件.通过这种方式,您可以创建无外观控件并使用 dependency properties.此外,使用该控件的人不需要了解相关的视图模型并使用它.

Case 2: If your control will be used by other applications/developers and you don't want to keep it simple then go ahead with creating custom controls following control template implementation. This way you can create look-less controls and use dependency properties too. Moreover whoever uses that control doesn't need to know about the related view model and use it.

一些类似的问题/帖子:

Some of the similar questions/posts:

WPF 设计问题(自定义控件或mvvm):WPF 设计问题(自定义控件或 mvvm)

WPF design question (custom control or mvvm): WPF design question (custom control or mvvm)

WPF 中使用 MVVM 的自定义控件概念:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6293b176-e1e9-4610-af49-d53e6d294969/

Custom control in WPF using MVVM concept: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6293b176-e1e9-4610-af49-d53e6d294969/

WPF 用户控制地狱与 MVVM 和依赖属性:WPF 用户控制地狱与 MVVM 和依赖属性

WPF User Control hell with MVVM and Dependency Properties: WPF User Control hell with MVVM and Dependency Properties

这篇关于Wpf 用户控件和 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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