结合WPF Expander和GridSplitter以及MinHeight

时间:2017-06-09 14:36:15

标签: c# wpf

GridSplitter如何能够很好地合并ExpanderMinHeight,同时尊重GridSplitter上方/下方两个区域的<Grid> <Grid.RowDefinitions> <RowDefinition Height="*" MinHeight="200" /> <!-- Main --> <RowDefinition Height="Auto" /> <!-- GridSplitter --> <RowDefinition Height="Auto" /> <!-- Expander --> </Grid.RowDefinitions> <ListBox> <ListBoxItem>A</ListBoxItem> <ListBoxItem>...</ListBoxItem> </ListBox> <GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" /> <Expander Grid.Row="2" Header="Expander" IsExpanded="False"> <ListBox MinHeight="150"> <ListBoxItem>X</ListBoxItem> <ListBoxItem>...</ListBoxItem> </ListBox> </Expander> </Grid>

例如:

Expander

如果我遗漏了GridSplitterMinHeight效果很好,并且尊重两个行定义的GridSplitter

如果我遗漏MinHeight(并将第二个Expander移至秒行定义),则MinHeight效果正常。

但是当两者都被使用时,Expander不受尊重,{{1}}就不再适用了。

有简单的解决方法吗?

1 个答案:

答案 0 :(得分:1)

RowDefinition上设置触发器,根据扩展器状态更改MinHeight

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" MinHeight="200" />
        <!-- Main -->
        <RowDefinition Height="Auto" />
        <!-- GridSplitter -->
        <RowDefinition Height="Auto" >
            <RowDefinition.Style>
                <Style TargetType="{x:Type RowDefinition}">
                    <Setter Property="MinHeight" Value="150" /> <!-- Default MinHeight -->
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=MyExpander, Path=IsExpanded}" Value="False">
                            <Setter Property="MinHeight" Value="24" /> <!-- MinHeight when Collapsed -->
                            <Setter Property="MaxHeight" Value="24" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
        <!-- Expander -->
    </Grid.RowDefinitions>

    <ListBox>
        <ListBoxItem>A</ListBoxItem>
        <ListBoxItem>...</ListBoxItem>
    </ListBox>

    <GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" />

    <Expander x:Name="MyExpander" Grid.Row="2" Header="Expander" IsExpanded="False">
        <ListBox>
            <ListBoxItem>X</ListBoxItem>
            <ListBoxItem>...</ListBoxItem>
        </ListBox>
    </Expander>
</Grid>

默认情况下,Row的MinHeight将为150,但是当Expander关闭时,它的最小和最大高度都设置为24(默认扩展器的高度),迫使它的高度保持在最小值值。