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

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

0
Just a learner 27 Фев 2020 в 21:03

2 ответа

Лучший ответ

Переместите 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>
1
mm8 28 Фев 2020 в 13:14

Это просто, установить 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
Rekshino 28 Фев 2020 в 13:41