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

vdzxcuhz  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(98)

我有两个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>

omtl5h9j

omtl5h9j1#

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

字符串

相关问题