Wpf ComboBox触发器

时间:2010-07-19 16:04:24

标签: wpf

我有两个ComboBoxes如下

<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>

我想要第一个是'All'来隐藏第二个使用Xaml并触发。

由于

1 个答案:

答案 0 :(得分:8)

   <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>