如何将视图模型属性公开为外部可绑定依赖属性?

发布于 2024-12-14 20:29:09 字数 721 浏览 1 评论 0原文

我正在编写一个内部使用 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:

Diagram of what 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-12-21 20:29:09

那么情况是,您有 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).

天涯沦落人 2024-12-21 20:29:09

如果我为 CustomControl 创建一个 ViewModel,则预计用户将使用该 ViewModel 作为 CustomControl.DataContext。在其他任何时候,我的 UserControl 都没有与其关联的 ViewModel。

例如,假设我想要一个 PopupControl。

我要么创建一个 UserControl,其中包含 UserControl 代码后面的所有自定义属性和逻辑(无 ViewModel)

<local:Popup Header="{Binding PopupHeader}"
             IsVisible="{Binding IsVisible}" />

或者

我会创建一个 < code>PopupViewModel 并构建我的 PopupUserControl,期望 DataContext 的类型为 PopupViewModel

<DataTemplate DataType="{x:Type local:PopupViewModel}">
    <local:PopupView />
</DataTemplate>

<ContentControl Content="{Binding MyPopupViewModel}" />

属性,如 IsVisible > 或PopupHeader 将存在于 ViewModel 中,而不是 PopupUserControl

If I'm creating a ViewModel for a CustomControl, it is expected that the user will use that ViewModel as the CustomControl.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 the UserControl (no ViewModel)

<local:Popup Header="{Binding PopupHeader}"
             IsVisible="{Binding IsVisible}" />

OR

I would create a PopupViewModel and build my PopupUserControl expecting that the DataContext will be of type PopupViewModel

<DataTemplate DataType="{x:Type local:PopupViewModel}">
    <local:PopupView />
</DataTemplate>

<ContentControl Content="{Binding MyPopupViewModel}" />

Properties like IsVisible or PopupHeader would exist in the ViewModel, not in the PopupUserControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文