1

我在 Border 控件中有一个 Expander 控件。当 Expander 折叠时,边框保持相同的高度。我如何使边框也改变它的高度?说明问题的代码:

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.Resources>
        <Style TargetType="Border">
            <Setter Property="BorderBrush" Value="Red"/>
            <Setter Property="BorderThickness" Value="2" />
        </Style>
        <Style TargetType="StackPanel" x:Key="StackPanel1">
            <Setter Property="Orientation" Value="Vertical"/>
            <Setter Property="MaxHeight" Value="100" />
        </Style>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" x:Key="panelBackgroundBrush">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#FFE3EFFF" Offset="0" />
                <GradientStop Color="#FFAFD2FF" Offset="1" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <Style TargetType="{x:Type GridSplitter}">
            <Setter Property="FrameworkElement.Height" Value="3"/>
            <Setter Property="UIElement.SnapsToDevicePixels" Value="True" />
            <Setter Property="UIElement.Focusable" Value="False" />
            <Setter Property="FrameworkElement.Cursor" Value="SizeNS"/>
            <Setter Property="VerticalAlignment" Value="Top"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border>
                            <Border BorderThickness="0" BorderBrush="{StaticResource panelBackgroundBrush}">
                                <Canvas Width="19" Height="3">
                                    <Canvas.Resources>
                                        <Style TargetType="Rectangle">
                                            <Setter Property="Width" Value="2" />
                                            <Setter Property="Height" Value="2" />
                                            <Setter Property="Canvas.Top" Value="0" />
                                        </Style>
                                    </Canvas.Resources>
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="1" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="5" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="9" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="13" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="17" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="0" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="4" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="8" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="12" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="16" />
                                </Canvas>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Border>
        <Expander>
            <Border>
                <StackPanel Style="{StaticResource ResourceKey=StackPanel1}" >
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                </StackPanel>
            </Border>
        </Expander>
    </Border>
    <Border Grid.Row="1">
        <Expander>
            <Border>
                <StackPanel Style="{StaticResource ResourceKey=StackPanel1}" >
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                </StackPanel>
            </Border>
        </Expander>
    </Border>
    <GridSplitter Grid.Row="1" />
    <Border Grid.Row="2">
        <Expander>
            <Border>
                <StackPanel Style="{StaticResource ResourceKey=StackPanel1}" >
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                </StackPanel>
            </Border>
        </Expander>
    </Border>
    <GridSplitter Grid.Row="2" />
</Grid>
4

2 回答 2

4

为了解决这个问题,必须将 RowDefinition.Height 设置为 Auto。

于 2012-11-26T17:58:36.420 回答
2

最好的方法是将边框设置为停靠面板的子项,并将停靠面板的 LastChildFill 属性设置为 true。这是一个例子:

<DockPanel LastChildFill="True">
    <Border>
        <Expander Name="PropertiesExpander2"
                  ExpandDirection="Left"
                  IsExpanded="False"
                  DockPanel.Dock="Right">
         </Expander>
    </Border>

    <Border>
              //Here you can add other controls
    </Border>
    </DockPanel>
于 2012-11-22T18:42:38.927 回答