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>

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

введите описание изображения здесь

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


person Just a learner    schedule 27.02.2020    source источник


Ответы (3)


Переместите 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>
person mm8    schedule 28.02.2020
comment
Спасибо! Это работает. GridSplitter разделяет только два элемента слева направо или вверх вниз? Кажется, если у GridSplitter более 3 братьев и сестер (включая себя), это не сработает. - person Just a learner; 28.02.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>
person Rekshino    schedule 28.02.2020

Я обнаружил, что проблема не в том, что GridSplitter может разделить только два до и после братьев и сестер, это неправильное определение RowDefinition! В фрагменте кода вопроса для первого и четвертого RowDefinition установлено значение Height="*", что заставляет их равномерно разделить дополнительное пространство. Вот почему, когда вы перетаскиваете разделитель, первая и четвертая строки всегда сохраняют одинаковую высоту. Если я изменю их в соответствии со следующей настройкой, все будет работать должным образом.

   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150" />
            <RowDefinition Height="5" />
            <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>

Так что больше не нужно гнездиться.

person Just a learner    schedule 04.04.2021