WPF GridSplitter с несколькими братьями и сестрами не работает должным образом - PullRequest
0 голосов
/ 27 февраля 2020

Я искал вокруг и не нашел подобного вопроса на форуме. У меня есть следующий код 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>

Когда пользователь перетаскивает разделитель сетки, только два текстовых поля должны быть изменены. Но то, что я получил, выглядит так:

enter image description here

Как я могу это исправить?

Ответы [ 2 ]

1 голос
/ 28 февраля 2020

Это просто, установите VerticalAlignment="Center" с помощью 'StackPanel´

<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" VerticalAlignment="Center">
        <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>
1 голос
/ 28 февраля 2020

Переместите 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>
...