需要绑定到父的某些属性,试验记录一下 结果
基本上,您需要使用RelativeSource绑定(bind)。
标准方法是查找特定类型控件的祖先(或父代):
{Binding DataContext.PropertyName, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type YourViewsNamespace:YourParentView}}}
假设您的父 View 的 View 模型设置为其DataContext,则此绑定(bind)将对其进行访问… DataContext是该 View 的DataContext,例如。设置为DataContext的 View 模型。因此,PropertyName属性是该 View 模型的公共(public)属性。
比如
<ComboBox Grid.Column="3" Width="120" ItemsSource="{Binding DataContext.SfList,RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}}" DisplayMemberPath="SF_ID" />
<Button IsEnabled="{Binding DataContext.IsWork,RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor},Converter={StaticResource Boolean2BooleanReConverter}}" Content="PLC" Command="{Binding PCommand}" />
这里的Combox 就是依据某个参数的值“3”,绑定生成的。
里面的ComboBox绑定的数据如上所示
<DataTrigger Binding="{Binding DataContext.DependencyProperty,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:UserControlName}}}" Value="On">
<Setter Property="Fill" Value="Red" />
</DataTrigger>