如何将视图模型属性公开为外部可绑定依赖属性?
我正在编写一个内部使用 MVVM 的 WPF 用户控件。为了开箱即用,它创建了自己的视图模型(这部分与问题无关)。
我的控件公开了许多依赖属性。主机视图(无论谁使用我的控件)都可以设置这些 DP 或绑定到它们。其中一些 DP 具有相应的视图模型属性。
这就是我想要实现的目标(见图):控件视图中的 DP 绑定到视图模型的相应属性。此外,主机视图也需要能够设置或绑定这些 DP - 与使用任何其他控件的方式相同。
但是,一旦宿主视图设置或绑定 DP 的值,原始绑定(将其内部绑定到控件的视图模型)就会丢失。这是因为每个 DP 只能有一个绑定集。所以这不起作用:
一种解决方案是使视图模型成为 DependencyObject 并它的属性 DP。这样,我可以将控件的视图模型绑定到其视图,而不是相反,从而有效地反转向左箭头。但是,由于各种原因,我希望我的视图模型使用纯 C# 属性和 INotifyPropertyChanged。
关于如何将视图模型属性公开为外部可绑定依赖属性有什么想法吗?
编辑:
澄清要求:视图实际上并不创建视图模型本身(我意识到这会违反关注点分离);相反,它使用视图模型定位器来执行此操作。 (这是一个非常模块化的企业应用程序。)我知道这不是典型的方法(请参阅 Rachel 的回答)。但是,接受所描述的设置,有什么方法可以达到预期的结果吗?
I'm writing a WPF user control which internally uses MVVM. In order to work out-of-the-box, it creates its own view-model (this part is not relevant to the question).
My control exposes a number of dependency properties. The host view (whoever uses my control) can set these DPs or bind to them. Some of these DPs have corresponding view-model properties.
This is what I want to achieve (see diagram): the DPs in my control's view are bound to the corresponding properties of the view-model. Additionally, the host view needs to be able to set or bind these DPs, too - the same way you use any other control.
However, as soon as the host view sets or binds the DP's value, the original binding (binding it internally to the control's view-model) is lost. This is because every DP can only have a single binding set. So this doesn't work:
One solution is to make the view-model a DependencyObject and its properties DPs. This way, I could bind the control's view-model to its view rather than the other way around, effectively reversing the left arrow. However, for various reasons, I want my view-model to use plain C# properties and INotifyPropertyChanged.
Any ideas on how to expose view-model properties as externally bindable dependency properties?
Edit:
To clarify on the requirements: the view does not in fact create the view-model itself (I realize that would violate the separation of concerns); rather, it uses a view-model locator to do so. (It's a terribly modular enterprise application.) I know this is not a typical approach (see Rachel's answer). However, accepting the described set-up, is there any way to achieve the desired result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么情况是,您有 VM 和 View,它们必须是彼此绑定的数据,而 VM 不能是
DependencyObject
?嗯,三重绑定 VM-V-Host 是不可能的,AFAIK。一种可能的方法是在视图中复制一组
DependencyProperties
。原始属性将绑定到 VM 并影响视图的外观,重复属性将绑定到 Host 并以某种方式影响 VM(例如,使用 DP 更改逻辑)。So the situation is that you have VM and View, they must be data bound to each other and VM can`t be
DependencyObject
? Well, triple binding VM-V-Host is not possible, AFAIK.One possible approach is to make duplicate set of
DependencyProperties
in View. Original properties will be bound to VM and will affect the look of View, duplicate properties will be bound to Host and somehow affect the VM (with DP changing logic, for example).如果我为
CustomControl
创建一个ViewModel
,则预计用户将使用该 ViewModel 作为CustomControl.DataContext
。在其他任何时候,我的 UserControl 都没有与其关联的 ViewModel。例如,假设我想要一个 PopupControl。
我要么创建一个
UserControl
,其中包含UserControl
代码后面的所有自定义属性和逻辑(无 ViewModel)或者
我会创建一个 < code>PopupViewModel 并构建我的
PopupUserControl
,期望DataContext
的类型为PopupViewModel
属性,如
IsVisible
> 或PopupHeader
将存在于ViewModel
中,而不是PopupUserControl
中If I'm creating a
ViewModel
for aCustomControl
, it is expected that the user will use that ViewModel as theCustomControl.DataContext
. Any other time, my UserControl's don't have ViewModels associated with them.For example, suppose I wanted a PopupControl.
I would either create a
UserControl
with all custom properties and logic in the code-behind theUserControl
(no ViewModel)OR
I would create a
PopupViewModel
and build myPopupUserControl
expecting that theDataContext
will be of typePopupViewModel
Properties like
IsVisible
orPopupHeader
would exist in theViewModel
, not in thePopupUserControl