仅调整打开的扩展器的大小

时间:2013-03-26 20:24:46

标签: wpf xaml expander window-resize

我在一个窗口中有3个扩展器,我想知道是否有办法让它如果我有扩展器2和3打开​​而不是1它只会拉伸那些2.它当前拉伸所有三个相同。此外,当它们关闭时,内容会关闭,但列保持与打开时相同的宽度。

让它们在展开时仅拉伸的最佳方法是什么?折叠时返回10像素宽的小尺寸?

1 个答案:

答案 0 :(得分:4)

您可以在Triggers上设置ColumnDefinitions,以便在Width关闭时将Expander设置为固定,并在Expander开启时设置为比例

示例:

     <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition>
                <ColumnDefinition.Style>
                    <Style TargetType="ColumnDefinition">
                        <Setter Property="Width" Value="20" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsExpanded, ElementName=expander1}" Value="True">
                                <Setter Property="Width" Value="20*" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ColumnDefinition.Style>
            </ColumnDefinition>
            <ColumnDefinition>
                <ColumnDefinition.Style>
                    <Style TargetType="ColumnDefinition">
                        <Setter Property="Width" Value="20" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsExpanded, ElementName=expander2}" Value="True">
                                <Setter Property="Width" Value="20*" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ColumnDefinition.Style>
            </ColumnDefinition>
            <ColumnDefinition>
                <ColumnDefinition.Style>
                    <Style TargetType="ColumnDefinition">
                        <Setter Property="Width" Value="20" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsExpanded, ElementName=expander3}" Value="True">
                                <Setter Property="Width" Value="20*" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ColumnDefinition.Style>
            </ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Expander Name="expander1" Grid.Column="0" Header="Expander1" />
        <Expander Name="expander2" Grid.Column="1" Header="Expander2" />
        <Expander Name="expander3" Grid.Column="2" Header="Expander3" />
    </Grid>

结果:

enter image description here enter image description here

enter image description here enter image description here