A191919
A191919

Reputation: 3442

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>

Upvotes: 2

Views: 2253

Answers (2)

YuvShap
YuvShap

Reputation: 3835

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.

Upvotes: 4

user1618054
user1618054

Reputation:

Control.Visibility is not needed because you specify a target type for the style. The problem is most likely that the trigger cannot find the binding via the element's name. It usually helps to have a single property in your view model that can be bound to multiple elements in your view.

<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"
    x:Name="PART_MainWindow">
    <Window.Resources>
        <Style TargetType="Label" x:Key="Test">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Visibility" Value="Visible"/>
                    <Setter Property="Background" Value="Red"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid DataContext="{Binding ElementName=PART_MainWindow}">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel>
            <RadioButton Content="1" IsChecked="{Binding IsChecked}"/>
            <RadioButton Content="2" IsChecked="{Binding IsChecked2}"/>
            <RadioButton Content="3" IsChecked="{Binding IsChecked3}"/>
            <RadioButton Content="4" IsChecked="{Binding IsChecked4}"/>
            <RadioButton Content="5" IsChecked="{Binding IsChecked5}"/>
        </StackPanel>
        <StackPanel Grid.Row="1">
            <Label Content="1" Style="{DynamicResource Test}" Visibility="Hidden"/>
            <Label Content="2" Visibility="Collapsed"/>
        </StackPanel>
    </Grid>
</Window>

Notes:

  • Bindings that use ElementName don't work properly when contained in a ResourceDictionary.
  • Bindings in a ResourceDictionary can be achieved either a) using a binding proxy or b) referencing an ancestor type.
  • You must specify a data context on the view container (in this case, the window).

Upvotes: 0

Related Questions