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

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

问题描述

我的想法写一个WPF用户控件我的应用程序。我使用MVVM在我的应用程序。

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

用户控制的需要,可以设置我的父视图依赖属性。使用MVVM的时候,这个想法是,父视图最终将创建用户控件 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)

依赖属性需要在View类的虚拟机创建不从的DependencyObject 继承。这意味着在后面的XAML code增加code。

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.

我在想,如果你能提供建议,如何开发使用WPF应用程序时,我应该设计一个用户控件MVVM ...

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

推荐答案

案例1:如果您正在创建这种控制只是为了在你的应用程序使用,那么你可以继续前进,创建一个视图模型吧,但你并不需要创建DP的,你的视图模型可以只实施 INotifyPropertyChanged的和您的父虚拟机仍然可以绑定到他们。

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.

在我们的例子中,对于用户控件,我们已经创建了独立的虚拟机和它的一个实例是present在 ParentVM 。所以父视图都会有这样的控制,并会结合 UserControlVM 此控件(ParentVM.UserControlVM <$ C C $>)和用户控件将采取其他绑定的照顾。

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::如果您的控制将被用于其他应用程序/开发人员,你不想保持简单,然后用下面的控制模板实现创建自定义控件继续。这样,您就可以创建查找较少的控制和使用依赖属性太。此外凡使用该控件并不需要了解相关的视图模型,并使用它。

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)

自定义使用MVVM WPF控制   概念:   <一href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6293b176-e1e9-4610-af49-d53e6d294969/" rel="nofollow">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和地狱   依赖属性:   <一href="http://stackoverflow.com/questions/2637662/wpf-user-control-hell-with-mvvm-and-dependency-properties">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天全站免登陆