13

Trying to show a label only when a certain item in a combo is selected. Code should pretty much explain it.

    <ComboBox Name="comboMyCombo">
        <ComboBoxItem>Don't show the label</ComboBoxItem>
        <ComboBoxItem>Show the label</ComboBoxItem>
    </ComboBox>

    <Label Visibility="Collapsed">This is my label
        <Label.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger 
                            Binding="{Binding ElementName=comboMyCombo, Path=SelectedValue}" Value="Show the label">
                        <Setter Property="Label.Visibility" Value="Visible"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
tim
  • 155
  • 1
  • 1
  • 7

2 Answers2

32

There are two issues here. First the default visibility should be specified in the style. But even with that it won't work because the binding on the trigger is comparing a SelectedValue, a ComboBoxItem object with a string object and that will never be equivalent. To keep the example simple, I've placed appropriate values in the ComboBoxItem's Tag properties. Although the actual implementation of the comparison will likely vary based on the specific needs of the app.

    <ComboBox Name="comboMyCombo">
        <ComboBoxItem Tag="Hide">Don't show the label</ComboBoxItem>
        <ComboBoxItem Tag="Show">Show the label</ComboBoxItem>
    </ComboBox>

    <Label>This is my label
        <Label.Style>
            <Style>
                <Setter Property="Label.Visibility" Value="Collapsed"></Setter>
                <Style.Triggers>
                    <DataTrigger  
                        Binding="{Binding ElementName=comboMyCombo, Path=SelectedItem.Tag}" Value="Show">
                        <Setter Property="Label.Visibility" Value="Visible"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
Scott J
  • 1,331
  • 9
  • 11
  • BTW: is there a way to reuse this trigger across multiple controls without repeating it? I have multiple controls I want to hide/based on this selection. They are separate grid cells so I can't just hide a whole stack panel. – tim Apr 01 '10 at 19:08
  • If you mean across multiple control types (Label, Button, etc.) I'd do that with an attached behavior. If you meant reused between different instances of the same control type, you should make the style a resource. – Scott J Apr 01 '10 at 19:26
  • Attached behavior. Thanks Scott, you are awesome. – tim Apr 01 '10 at 19:37
12

A "cleaner" solution would be

<ComboBox>
    <ComboBoxItem x:Name="iOne" Content="One"/>
    <ComboBoxItem x:Name="iTwo" Content="Two"/>
    <ComboBoxItem x:Name="iThree" Content="Three"/>
</ComboBox>

<Label Content="One is shown">
 <Label.Style>
    <Style TargetType="Label">
        <Setter Property="Visibility" Value="Hidden" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=iOne, Path=IsSelected}"    Value="True">
                <Setter Property="Visibility"  Value="Visible"/>
            </DataTrigger> 
        </Style.Triggers>
    </Style>
 </Label.Style>
</Label>
edvaldig
  • 2,301
  • 16
  • 17