A191919 A191919 - 1 month ago 20
C# Question

WPF Visibility in MultiDataTrigger

Why

<Setter Value="Visible" Property="Control.Visibility"/>
do not make label visible when
RadioButton1
IsChecked=true
?

<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Label" x:Key="Test">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsChecked, ElementName=RadioButton1}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Value="Visible" Property="Control.Visibility"></Setter>
<Setter Property="Background" Value="Red"></Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<RadioButton Content="1" x:Name="RadioButton1" IsChecked="True"></RadioButton>
<RadioButton Content="2" ></RadioButton>
<RadioButton Content="3" x:Name="RadioButton3"></RadioButton>
<RadioButton Content="4"></RadioButton>
<RadioButton Content="5" x:Name="RadioButton5"></RadioButton>
</StackPanel>
<StackPanel Grid.Row="1">
<Label Content="1" Style="{DynamicResource Test}" Visibility="Hidden"></Label>
<Label Content="2" Visibility="Collapsed"></Label>
</StackPanel>
</Grid>



Answer

Maybe you can try to set the label visibility default value (Hidden) in the style:

<Style TargetType="Label" x:Key="Test">
        <Setter Property="Visibility" Value="Hidden"/>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding IsChecked, ElementName=RadioButton1}" Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Visibility" Value="Visible"/>
                <Setter Property="Background" Value="Red"/>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

And not in it's declaration:

<Label Content="1"  Style="{StaticResource Test}"/>

When you set the lable visibilty in it's declaration the lable's value cannot be changed using style triggers.