Gridsplitter没有调整大小

时间:2016-03-21 17:36:40

标签: wpf treeview

我希望GridSplitter使TreeView更大,以便更容易看到TreeView的内容。似乎无法找到放置拆分器的正确位置

<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"/>

2 个答案:

答案 0 :(得分:2)

将其设置为与第二个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>

答案 1 :(得分:1)

只需将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定义了一个可以拉伸和缩放a的内容装饰器   单身孩子填补空间。

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>