公开 MVVM 中内置的用户控件的属性 [英] Expose the properties of a usercontrol which is built in MVVM

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

问题描述

像这样的问题有很多,但仍然无法得到我真正想要的,而且所有这些问题都有一些与我不同的东西,那就是:我有一个 UserControl:

There are lots of questions like this one, but still can't get what I really want, and all of them have something that differs of mine and that is: I have a UserControl:

  • 单独构建在名为UCProject的类库项目中;
  • 项目UCProject有一个View用于控件,以及它的ViewModel
  • UserControl 将自己在 View 中的一些控件和 UIElements 属性绑定到在 ViewModel 中声明的属性,当然,在 UCProject 中;
  • 如何显示或公开或使 ViewModel 属性可访问可能承载此 UserControl 的页面或项目窗口(例如可以称为 GlobalProject);
  • Built separately in a class library project called UCProject;
  • The project UCProject has a View for the control, and its ViewModel;
  • The UserControl binds some of its own controls and UIElements properties in the View to properties declared in the ViewModel in, of course, the UCProject;
  • How can I show or expose or make the ViewModel Properties accessible to the page or the window of the project (that may be called for instance GlobalProject) that may host this UserControl;

我正在构建这个 UserControl,我想为它构建事件、属性......并且我希望它被其他人用作给定的程序集给他们,所以它的代码不是可访问,他们只能使用它,我想尊重 MVVM 模式,我不知道如何实现这一点,我应该在 UserControl 的 CodeBehind 中编写这些属性和事件View还是应该放在ViewModel里面,那怎么从外面访问,就像我们日常使用的第三方控件一样

I'am building this UserControl, I want to build events, properties to it ... and I want it to be used by others as a given assembly to them, so its code is not accessible, they only can consume it, I want to respect the MVVM pattern, and I don't have a clear idea how to realize that, should I write this properties and events in the CodeBehind of the UserControl View or should I put them in the ViewModel, and in that case how can I access them from outside, just like we daily use third-party controls

推荐答案

如果我认为是正确的,您希望将子 UserControl 的 ViewModel 的属性公开到包含可能是窗口或其他 UserControl 的父根元素.

If i assume it right, you want to expose properties of child UserControl's ViewModel to containing parent root element which might be window or other UserControl.

有两种方法:

首先,DataContext DP 是可继承的,即子控件从父控件继承它,除非将其显式设置为其他值.因此,您可以做的是拥有 common ViewModel 并将其设置为父 UserControl 上的 DataContext,并且两者都可以访问其属性.

First, DataContext DP is inheritable i.e. child controls inherit it from parent control unless set it explicitly to some other value. So, what you can do is have common ViewModel and set it as DataContext on parent UserControl and both have access to its properties.

其次,如果您想要为父用户控件和子用户控件单独设置 ViewModel.您始终可以通过DataContext访问子视图模型的属性.让我用一个例子来解释:

Second, in case you want separate ViewModels for parent and child UserControls. You can always access properties of child's ViewModel via DataContext. Let me explain with an example:

<UserControl x:Name="ParentUserControl">
   <StackPanel>
      <local:ChildUserControl x:Name="Child"/>
      <TextBlock Text="{Binding DataContext.PropertyName, ElementName=Child}"/>
   </StackPanel>
</UserControl>

其中 PropertyName 是 ChildUserControl 的 ViewModel 中的属性.

where PropertyName is property in ViewModel of ChildUserControl.

在这里,您可以看到位于 ParentUserControl 中的 TextBlock 绑定到子 UserControl 的 ViewModel 中的属性.

Here as you can see TextBlock which lies in ParentUserControl is binding to property in ViewModel of child UserControl.

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

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