【问题标题】:How to bind Parent UserControl Dependency Property to the Child UserControl ViewModel Property?如何将父用户控件依赖属性绑定到子用户控件 ViewModel 属性?
【发布时间】:2012-08-03 04:44:43
【问题描述】:

您好,我有 2 个用户控件。 Parent UserControl 和 Child UserControl 都有自己的 ViewModel。如何将父控件的依赖属性绑定到子控件的 ViewModel 中的属性。任何帮助都是显而易见的。我可以轻松地将 Parent UserControl DependencyProperty 绑定到 Child Control Dependency Property 。但问题是如何将其绑定到 ViewModel 属性。假设我将 Child UserControl 的 Tag 属性绑定到 ParentControl 的 Dependency 属性(到这里为止很好),但是现在我如何将此 Tag 属性绑定到 ViewModel 属性。(我想保持简单,不想使用 Converter/转换器参数逻辑)。提前致谢。

【问题讨论】:

    标签: wpf c#-4.0 binding


    【解决方案1】:

    通常您的ViewModels 是您的UserControlsDataContext(数据层),因此您可以通过绑定到DataContext 属性来访问子控件的ViewModel

    {Binding ElementName=MyChildUserControl, Path=DataContext.ChildViewModelProperty}
    

    【讨论】:

      【解决方案2】:

      试试类似的东西

      {Binding Path=PathToParentDP, RelativeSource={RelativeSource AncestorType={x:Type ParentType}}}
      

      【讨论】:

      • 谢谢,是的,它工作正常,我将上面的绑定绑定到 ChildControl 的 Dependency 属性,但是我如何将子控件绑定到 ViewModel 属性,因为我们不能做绑定/赋值同一属性两次。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多