我的观点是这样的。对于用户检查 chk1 时需要的测试, chk2 元素将属性 IsEnabled 更改为 False ,但是我不能参考chk2元素。
这是Style XAML。
<Style x:Key="styleCheckBox" TargetType="{x:Type CheckBox}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
</Style.Triggers>
</Style
致电Style ..
<StackPanel>
<CheckBox x:Name="chk1" Content="CheckBox1" Style="{StaticResource styleCheckBox}"/>
<CheckBox x:Name="chk2" Content="CheckBox2"/>
</StackPanel>
答案 0 :(得分:3)
您无法在样式触发器中设置TargetProperty。这基本上意味着您应该创建一个派生自StackPanel的自定义控件,它包含两个复选框,这些复选框作为属性公开。然后,您将能够为该控件(而不是CheckBox)定义样式并设置所需的属性。
更简单的方法(如果只需要测试)就是这样:
<StackPanel>
<StackPanel.Resources>
<local:InverseBoolConverter x:Key="InverseBoolConverter"/>
</StackPanel.Resources>
<CheckBox x:Name="chk1" Content="CheckBox1"/>
<CheckBox x:Name="chk2" Content="CheckBox2" IsEnabled="{Binding ElementName=chk1, Path=IsChecked, Converter={StaticResource InverseBoolConverter}}"/>
</StackPanel>
InverseBoolConverter的定义如下:
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBoolConverter: IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if(value is bool)
return !(bool)value;
else
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if(value is bool)
return !(bool)value;
else
return null;
}
}