GridSplitter 的奇怪行为

Strange behavior of GridSplitter


可以说我是WPF的初学者

我对 GridSplitter 有一种奇怪的行为;网格被分成5行w.

  1. 标签栏
  2. 水平网格
  3. 分离器
  4. 另一个标签栏
  5. 文本框 (txtLog)

就像下面的代码,但是当我移动分隔符时,txtLog 会向下移动,而不是跟随上一个标签栏。

XAML:

<Window x:Class="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:MSCSimulator"
    mc:Ignorable="d"
    FontSize="14"
    Title="Simulator" 
    Height="500" Width="800" 
    WindowStartupLocation="CenterScreen" 
    WindowStyle="ThreeDBorderWindow">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" MinHeight="50" />
        <RowDefinition Height="*" MinHeight="120" />
        <RowDefinition Height="10" />
        <RowDefinition Height="50" MinHeight="50" />
        <RowDefinition Height="*" MinHeight="120" />
    </Grid.RowDefinitions>
    <!-- TOOLBAR -->
    <ToolBarTray Margin="10,10,10,10" Width="Auto" Height="30">
        <ToolBar Width="Auto" Height="30">
            <Button Click="Send_Click">
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Simulator;component/Images/play.png" Width="12" Height="12" />
                    <Label Padding="5,0,0,0">Invia</Label>
                </StackPanel>
            </Button>
        </ToolBar>
    </ToolBarTray>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="220" MinWidth="150" MaxWidth="220" />
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TreeView Width="Auto" Margin="10,0,0,0">
            <TreeViewItem Header="Commands" IsExpanded="True">
                <!-- APPLICATION -->
                <TreeViewItem Header="Application">
                    <TreeViewItem Header="Simulate" MouseDoubleClick="Application_Simulate_MouseDoubleClick" />
                    <TreeViewItem Header="Update" MouseDoubleClick="Application_Update_MouseDoubleClick" />
                </TreeViewItem>
            </TreeViewItem>
            <TreeView.Resources>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter Property="FontWeight" Value="Normal"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.Resources>
        </TreeView>
        <GridSplitter Width="10" 
                      Background="White" 
                      VerticalAlignment="Stretch" 
                      ResizeBehavior="BasedOnAlignment" />
        <!-- JSON -->
        <TextBox Name="txtJSON" 
                 Grid.Column="2" 
                 Background="White" 
                 TextWrapping="Wrap" 
                 AcceptsReturn="True" 
                 Margin="-10,0,10,0"
                 VerticalScrollBarVisibility="Visible"
                 Width="Auto" Height="Auto" />
    </Grid>
    <GridSplitter Grid.Row="2" 
                  Height="10" 
                  ResizeDirection="Rows"
                  HorizontalAlignment="Stretch"
                  ResizeBehavior="PreviousAndNext"
                  Background="White"/>
    <!-- TOOLBAR -->
    <ToolBarTray Grid.Row="3" Margin="10,0,10,0" Width="Auto" Height="30" VerticalAlignment="Top">
        <ToolBar Width="Auto" Height="30">
            <Button Click="Clear_Click">
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Simulator;component/Images/clear.png" Width="16" Height="16" />
                    <Label Padding="5,0,0,0">Cancella</Label>
                </StackPanel>
            </Button>
        </ToolBar>
    </ToolBarTray>
    <!-- LOGGER-->
    <TextBox Name="txtLog" 
             Grid.Row="4" 
             TextWrapping="Wrap" 
             Margin="10,-10,10,10" 
             AcceptsReturn="True" 
             VerticalScrollBarVisibility="Auto" 
             Width="Auto" Height="Auto" 
             IsReadOnly="True">
        <TextBox.Background>
            <SolidColorBrush Color="#282828"></SolidColorBrush>
        </TextBox.Background>
        <TextBox.Foreground>
            <SolidColorBrush Color="White"></SolidColorBrush>
        </TextBox.Foreground>
    </TextBox>
</Grid>

提前致谢

在你的外部 Grid 中,尝试将高度从 * 到 Auto 放在第二个 RowDefinition

...
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" MinHeight="50" />
        <RowDefinition Height="Auto" MinHeight="120" />
        <RowDefinition Height="10" />
        <RowDefinition Height="50" MinHeight="50"/>
        <RowDefinition Height="*" MinHeight="120" />
    </Grid.RowDefinitions>
    <!-- TOOLBAR -->
...