如何在另一个元素发生变化时触发 Setter

问题描述 投票:0回答:1

我有两个

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>
wpf xaml triggers combobox
1个回答
12
投票
   <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>
© www.soinside.com 2019 - 2024. All rights reserved.