我有两个
ComboBoxes
,如下:
当第一个框选择为“All”时,应使用 XAML 和触发器隐藏第二个框。
<StackPanel Orientation="Horizontal">
<ComboBox x:Name="cbxOne" Style="{StaticResource demoStyle}">
<ComboBoxItem >One</ComboBoxItem>
<ComboBoxItem >Two</ComboBoxItem>
<ComboBoxItem >All</ComboBoxItem>
</ComboBox>
<ComboBox x:Name="cbxTwo">
<ComboBoxItem >1</ComboBoxItem>
<ComboBoxItem >2</ComboBoxItem>
</ComboBox>
</StackPanel>
我尝试了这种风格:
<Style x:Key="demoStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="SelectedValue" Value="All">
<Setter Property="cbxTwo.Visibility" Value="Collapsed"></Setter>
</Trigger>
</Style.Triggers>
</Style>
<StackPanel>
<ComboBox Name="cbxOne">
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
<ComboBoxItem>All</ComboBoxItem>
</ComboBox>
<ComboBox>
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedItem.Content, ElementName=cbxOne}" Value="All">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</StackPanel>