首页 > 解决方案 > 具有多个兄弟姐妹的 WPF GridSplitter 无法按预期工作

问题描述

我四处搜索,并没有在论坛上找到类似的问题。我有以下 WPF 代码。

<Window x:Class="WpfApp5.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:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="238.788" Width="406.407">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBox TextWrapping="Wrap" Text="TextBox1 should be resized while moving the GridSplitter"/>
        <GridSplitter HorizontalAlignment="Stretch" Height="5"  Grid.Row="1" />
        <StackPanel Grid.Row="2" Orientation="Horizontal" Background="Black">
            <TextBlock Padding="5" Text="I want this black section have fixed height while moving the GridSplitter" Foreground="Aqua" VerticalAlignment="Center" />
        </StackPanel>
        <TextBox Grid.Row="3" TextWrapping="Wrap" Text="TextBox2 should be resized while moving the GridSplitter"/>
    </Grid>
</Window>

当用户拖动网格拆分器时,只有两个文本框应该调整大小。但我得到的是这样的:

在此处输入图像描述

我怎样才能解决这个问题?

标签: wpfgridsplitter

解决方案


StackPanel第二个和第二个移动TextBox到一个Panel

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox TextWrapping="Wrap" Text="TextBox1 should be resized while moving the GridSplitter"/>
    <GridSplitter HorizontalAlignment="Stretch" Height="5"  Grid.Row="1" />
    <DockPanel Grid.Row="2">
        <StackPanel Orientation="Horizontal" Background="Black" DockPanel.Dock="Top">
            <TextBlock Padding="5" Text="I want this black section have fixed height while moving the GridSplitter" Foreground="Aqua" VerticalAlignment="Center" />
        </StackPanel>
        <TextBox TextWrapping="Wrap" Text="TextBox2 should be resized while moving the GridSplitter"/>
    </DockPanel>
</Grid>

推荐阅读