Gridsplitter 不调整大小

Gridsplitter not resizing

我想让GridSplitter 把TreeView 变大,以便更容易看到TreeView 的内容。似乎找不到放置 Splitter 的正确位置

<Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition Width="auto"/>
    <ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
    <RowDefinition Height="auto"/>
    <RowDefinition/>
    <RowDefinition Height="auto"/>
    <RowDefinition />
    <RowDefinition Height="auto"/>
    <RowDefinition Height="auto"/>
</Grid.RowDefinitions>

<TextBlock Grid.Column="0" Grid.Row="0" Text="JSON String" Margin="5"/>
<TextBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3"/>

<GridSplitter Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2" Height="5"         ResizeDirection="Rows" ResizeBehavior="PreviousAndNext" HorizontalAlignment="Stretch"/>

<TextBlock Grid.Column="0" Grid.Row="3" Text="C# Type" Margin="5"/>
<TreeView Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="3" x:Name="outputTree" Margin="5"/>

只需将您的 TreeView 放入 ViewBox:

<Grid.RowDefinitions>
  <RowDefinition Height="auto"/>
  <RowDefinition/>
  <RowDefinition Height="auto"/>
  <RowDefinition MinHeight="1"/>
  <RowDefinition Height="auto"/>
  <RowDefinition Height="auto"/>
</Grid.RowDefinitions>

<ViewBox Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="3">
  <TreeView  x:Name="outputTree" Margin="5"/>
</ViewBox>

As MSDN says:

ViewBox defines a content decorator that can stretch and scale a single child to fill the available space.

This WPF tutorial is also helpful.

作品示例:

<Window x:Class="TreeViewWpfApplication.MainWindow"
        ...The code omitted for the brevity...
        Title="MainWindow" Height="350" Width="525">
    <Grid>     
            <Grid.RowDefinitions>
                <RowDefinition/>
            <RowDefinition Height="5" />
            <RowDefinition/>
            </Grid.RowDefinitions>
        <Viewbox>
            <TreeView Name="treeView">
                <TreeViewItem Header="1">
                    <TreeViewItem Header="1.2">
                        <TreeViewItem Header="1.3"/>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
            </TreeView>
        </Viewbox>
        <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" />
        <Button Grid.Row="2" Content="Hello" Name="btn"/>
    </Grid>
</Window>

更新:

我稍微编辑了你的代码。请参阅:

<Grid>
    <Grid.RowDefinitions>            
        <RowDefinition Height="auto"/>
        <RowDefinition/>
        <RowDefinition Height="auto"/>
        <RowDefinition />
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Column="0" Grid.Row="0" Text="JSON String" Margin="5"/>
    <TextBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3"/>

    <GridSplitter Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2" 
        Height="5" ResizeDirection="Rows" ResizeBehavior="PreviousAndNext" 
        HorizontalAlignment="Stretch"/>
    <Grid Grid.Row="3">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock  Text="C# Type" Margin="5"/>
        <Viewbox  Grid.Row="1" >
            <TreeView   x:Name="outputTree" Margin="5">
                <TreeViewItem Header="1">
                    <TreeViewItem Header="1.2">
                        <TreeViewItem Header="1.3"/>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
                <TreeViewItem Header="2"/>
            </TreeView>
        </Viewbox>
    </Grid>

    <TextBlock Text="5" Grid.Row="4"/>
    <TextBlock Text="Hello6" Grid.Row="5"/>
</Grid>

将其设置为与第二个 TextBlock 相同的行并将 ResizeBehavior 设置为 PreviousAndNext 我调整了一些行定义,因此请根据需要进行调整

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition />
        <RowDefinition Height="auto"/>
        <RowDefinition  />
    </Grid.RowDefinitions>

    <TextBlock Grid.Column="0" Grid.Row="0" Text="JSON String" Margin="5"/>
    <TextBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" Margin="5"/>


    <GridSplitter Grid.Column="0" Grid.ColumnSpan="3"  Grid.Row="2" Height="5"         ResizeDirection="Rows" ResizeBehavior="PreviousAndNext" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
    <TextBlock Grid.Column="0" Grid.Row="2" Text="C# Type" Margin="5"/>
    <TreeView Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" x:Name="outputTree" Margin="5"/>
</Grid>