GridSplitter的奇怪行为

时间:2019-05-29 10:07:57

标签: wpf grid gridsplitter rowdefinition


可以说我是WPF的初学者。

我的GridSplitter行为很奇怪;网格分为5行。

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

类似于下面的代码,但是当我移动分隔符时,txtLog向下移动,而不是跟随先前的选项卡。

enter image description here

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>

感谢前进

1 个答案:

答案 0 :(得分:1)

在外部网格中,尝试在第二个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 -->
...