【问题标题】:Set inherited Binding source for UserControl element为 UserControl 元素设置继承的绑定源
【发布时间】:2011-01-22 00:13:46
【问题描述】:

确实是简单的语法问题,但我无处可去:

(伪代码)

MainWindow.xaml:

<grid>
  <control:MyUserControl DataContext="{StaticResource MyDataSource}" />
</grid>

MyUserControl.xaml

<grid>
  <stackpanel DataContext="{StaticResource MyOtherDataSource}" IsEnabled="{Binding Path=CanUseMe, Source={StaticResource MyDataSource}" />
</grid>

问题是堆栈面板中的{StaticResource MyDataSource},因为用户控件没有此资源。

当我已经为堆栈面板设置了 DataContext 时,如何将源绑定设置为传递给用户控件的“全局”数据源?

谢谢!

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    UserControl 的 DataContext 将是 MyDataSource,因此您可以像这样在 Binding 中使用RelativeSource

    <grid>
        <stackpanel DataContext="{StaticResource MyOtherDataSource}"
                    IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
                                        Path=DataContext.CanUseMe}"/>
    </grid> 
    

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多