开发者

Set inherited Binding source for UserControl element

Simple syntax question really but I'm getting nowhere:

(Psuedo code)

MainWindow.xaml:

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

MyUserControl.xaml

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

The problem is {StaticResource MyDataSource} in the sta开发者_StackOverflowckpanel, since the user control doesn't have this resource.

How do I set the source binding to be the 'global' datasource passed into the usercontrol when I've already set the DataContext for the stackpanel?

Thanks!


The UserControl's DataContext will be MyDataSource, so you can use RelativeSource in the Binding like this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜