tags:

views:

189

answers:

1

My point is this. For test i need when user check chk1 the chk2 element changed the property IsEnabled to False, but i can't do reference to chk2 element.

This is Style XAML.

<Style x:Key="styleCheckBox" TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">

            </Style.Triggers>
 </Style

Call to Style..

 <StackPanel>
        <CheckBox x:Name="chk1" Content="CheckBox1" Style="{StaticResource styleCheckBox}"/>
        <CheckBox x:Name="chk2" Content="CheckBox2"/>
    </StackPanel>
+3  A: 

You cannot set TargetProperty in Style Trigger. This basically means that you should create a custom control derived from StackPanel which contains two checkboxes and these checkboxes are exposed as properties. Then you'll be able to define a style for that control (not the CheckBox) and set the properties you want.

Much easier way (if only needed for testing) would be this:

<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>

Where InverseBoolConverter is defined as follows:

[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;
    }
}
Stanislav Kniazev
Thanks!!, very useful.
Rangel