Wpf 为UserControl元素设置继承的绑定源
很简单的语法问题,但我什么也没学到: (Psuedo代码) MainWindow.xaml:Wpf 为UserControl元素设置继承的绑定源,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,很简单的语法问题,但我什么也没学到: (Psuedo代码) MainWindow.xaml: <grid> <control:MyUserControl DataContext="{StaticResource MyDataSource}" /> </grid> MyUserControl.xaml <grid> <stackpanel DataContext="{StaticResource MyOtherDataSource
<grid>
<control:MyUserControl DataContext="{StaticResource MyDataSource}" />
</grid>
MyUserControl.xaml
<grid>
<stackpanel DataContext="{StaticResource MyOtherDataSource}" IsEnabled="{Binding Path=CanUseMe, Source={StaticResource MyDataSource}" />
</grid>
问题是stackpanel中的{StaticResource MyDataSource}
,因为用户控件没有此资源
当我已经为stackpanel设置了DataContext时,如何将源绑定设置为传入usercontrol的“全局”数据源
谢谢 UserControl的数据上下文将是MyDataSource,因此您可以像这样在绑定中使用
RelativeSource
<grid>
<stackpanel DataContext="{StaticResource MyOtherDataSource}"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Path=DataContext.CanUseMe}"/>
</grid>
用户控件的DataContext将是MyDataSource,因此您可以像这样在绑定中使用
RelativeSource
<grid>
<stackpanel DataContext="{StaticResource MyOtherDataSource}"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Path=DataContext.CanUseMe}"/>
</grid>