This demonstrates two simple ways that you can use a style to change the visibility of elements based on a selection in a combo box. The first style checks SelectedIndexthe combo boxes, and the second checks it SelectedValue. In this example, I filled in the combo box with string objects, but you can use it SelectedValuewith any kind of object if you know that its method ToString()returns.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<DockPanel>
<ComboBox x:Name="comboBox" DockPanel.Dock="Top">
<system:String>Item 1</system:String>
<system:String>Item 2</system:String>
</ComboBox>
<TextBlock DockPanel.Dock="Top" Text="This displays if Item 1 is selected">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=comboBox, Path=SelectedIndex}" Value="0">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock DockPanel.Dock="Top" Text="This displays if Item 2 is selected.">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=comboBox, Path=SelectedValue}" Value="Item 2">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DockPanel>
</Page>
source
share