外扩张器坍塌时折叠内扩张器

时间:2017-09-08 07:38:12

标签: wpf xaml datatrigger expander

我试图在取消选中切换按钮时折叠扩展的扩展器。使用DataTriggers不起作用。我怎样才能做到这一点?为什么DataTrigger不起作用?

这是代码。

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <ToggleButton Name="TheToggle">Collapse expanders when unchecked</ToggleButton>
                    <Expander Header="Menu 1">
                        <Expander.Style>
                            <Style TargetType="Expander">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=TheToggle,Path=IsChecked}" Value="False">
                                        <Setter Property="IsExpanded" Value="False"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Expander.Style>
                        <StackPanel>
                            <Button>Menu Item 1</Button>
                            <Button>Menu Item 2</Button>
                            <Button>Menu Item 3</Button>
                            <Button>Menu Item 4</Button>
                        </StackPanel>
                    </Expander>
                    <Expander Header="Menu 2">
                        <StackPanel>
                            <Button>Menu Item 1</Button>
                            <Button>Menu Item 2</Button>
                            <Button>Menu Item 3</Button>
                            <Button>Menu Item 4</Button>
                        </StackPanel>
                    </Expander>
            <Expander Header="Menu 3">
                <StackPanel>
                    <Button>Menu Item 1</Button>
                    <Button>Menu Item 2</Button>
                    <Button>Menu Item 3</Button>
                    <Button>Menu Item 4</Button>
                </StackPanel>
            </Expander>
        </StackPanel>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:1)

我修改了你的例子以折叠/展开Buttonclick上的所有Expander:

  <Grid>
        <StackPanel>
            <ToggleButton Name="TheToggle" Content="Collapse expanders when unchecked">

            </ToggleButton>
            <Expander Header="Menu 1" IsExpanded="{Binding IsChecked, ElementName=TheToggle, Mode=TwoWay}">
                <StackPanel>
                    <Button>Menu Item 1</Button>
                    <Button>Menu Item 2</Button>
                    <Button>Menu Item 3</Button>
                    <Button>Menu Item 4</Button>
                </StackPanel>
            </Expander>
            <Expander Header="Menu 2" IsExpanded="{Binding IsChecked, ElementName=TheToggle, Mode=TwoWay}">
                <StackPanel>
                    <Button>Menu Item 1</Button>
                    <Button>Menu Item 2</Button>
                    <Button>Menu Item 3</Button>
                    <Button>Menu Item 4</Button>
                </StackPanel>
            </Expander>
            <Expander Header="Menu 3" IsExpanded="{Binding IsChecked, ElementName=TheToggle, Mode=TwoWay}">
                <StackPanel>
                    <Button>Menu Item 1</Button>
                    <Button>Menu Item 2</Button>
                    <Button>Menu Item 3</Button>
                    <Button>Menu Item 4</Button>
                </StackPanel>
            </Expander>
        </StackPanel>
    </Grid>

这是想要的行为吗?