2

我想让 GridSplitter 只调整一个单元格的大小(实际上,对于 WPF Grid 而言,它本身不是正确的术语,我们称它为单独的 Grid[r][c])和与它相邻的单元格以调整大小。

这是我尝试过的:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0">
            <TextBlock>Testing 1</TextBlock>
        </Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="1">
            <TextBlock>Testing 2</TextBlock> </Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="2">
            <TextBlock>Testing 2</TextBlock>
        </Border>
        <Border Grid.IsSharedSizeScope="True" Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="0">
            <TextBlock>Testing 3</TextBlock>
        </Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="1">
            <TextBlock>Testing 4</TextBlock></Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="2">
            <TextBlock>Testing 5</TextBlock></Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="3" Grid.Column="0">
            <TextBlock>Testing 6</TextBlock></Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="3" Grid.Column="1">
            <TextBlock>Testing 7</TextBlock></Border>
        <Border Grid.IsSharedSizeScope="True"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="3" Grid.Column="2">
            <TextBlock>Testing 8</TextBlock></Border>
        <GridSplitter Grid.Row="0"
                      Grid.Column="1"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Left" 
                        VerticalAlignment="Stretch"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="1"
                      Grid.Column="1"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Left" 
                        VerticalAlignment="Stretch"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="2" 
                      Grid.Column="1"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Left" 
                        VerticalAlignment="Stretch"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="2" 
                      Grid.Column="1"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"

                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="0" 
                      Grid.Column="2"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Left" 

                      VerticalAlignment="Stretch"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="1" 
                      Grid.Column="2"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Left" 
                        VerticalAlignment="Stretch"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="2" 
                      Grid.Column="2"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Left" 
                        VerticalAlignment="Stretch"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Columns"
                        Width="2"/>
        <GridSplitter Grid.Row="1"
                      Grid.Column="0"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Rows"
                        Height="2"/>
        <GridSplitter Grid.Row="2"
                      Grid.Column="0"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Rows"
                        Height="2"/>
        <GridSplitter Grid.Row="1" 
                      Grid.Column="1"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Rows"
                        Height="2"/>
        <GridSplitter Grid.Row="2" 
                      Grid.Column="1"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Rows"
                        Height="2"/>
        <GridSplitter Grid.Row="1" 
                      Grid.Column="2"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Rows"
                        Height="2"/>
        <GridSplitter Grid.Row="2" 
                      Grid.Column="2"
                      Margin="1"
                      ResizeBehavior="PreviousAndCurrent"
                      HorizontalAlignment="Stretch" 
                        VerticalAlignment="Top"
                        Background="Black" 
                        ShowsPreview="true"
                        ResizeDirection="Rows"
                        Height="2"/>
    </Grid>

我希望它的行为方式是调整网格中的单个单元格的大小。

  1. 在应用程序启动时

在这里它在应用程序开始时的样子

  1. 通过网格拆分器调整大小时:

调整大小时

  1. 调整大小后:

调整大小后

我希望只调整 Grid[0][0] 和 Grid[0][1] 的大小

4

2 回答 2

2

您有 3 列和 3 行,而不是每行内的 3 列,试试这个:

<Grid  >
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <GridSplitter Grid.Column="0"/>
        <GridSplitter Grid.Column="1"/>
        <GridSplitter Grid.Column="2"/>
    </Grid>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

    </Grid>
    <Grid Grid.Row="2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>   
    </Grid>
</Grid>
于 2017-04-17T18:59:04.220 回答
1

有自己的代码(对于“简单”,对不起),它解决了垂直网格分割问题。给定时间,将包括对水平的更新,但假设它只不过是定义不同的网格列而不是 Celso 的不同网格行。应用GridSizeBehaviour
的特定成员字段肯定存在一些问题。在第一个附加的垂直网格分割示例中,将CurrentAndNext 替换为BasedOnAlignment属性,以发现所有行都受到影响 - 而不仅仅是定义网格分割器的行。 要解决GridSizeBehaviour 问题,请将CurrentAndNext替换为PreviousAndCurrent

在那里进行第 0 行定义,并发现一旦单击 gridsplitter,它就永远不会返回/失去可见性,以及表单上的其余控件。

<Window x:Class="GridSplitterSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitterSample" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Left side</TextBlock>
    <GridSplitter Grid.Row="0"  Grid.Column="1" Width="5" HorizontalAlignment="Stretch" ResizeBehavior="CurrentAndNext" />
    <TextBlock Grid.Column="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Right side</TextBlock>
    <Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">Button 1</Button>
    <Button Grid.Row="2" Grid.Column="0">Button 2</Button>
    <Button Grid.Row="2" Grid.Column="2">Button 3</Button>
</Grid>

也许有一个API解决方案?
但是使用 Celso 的嵌套网格的想法,它可以在以下代码中运行:

<Window x:Class="GridSplitterSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitterSample" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
       <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*" />
       <ColumnDefinition Width="5" />
       <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Left side</TextBlock>
        <GridSplitter Grid.Row="0"  Grid.Column="1" Width="5" HorizontalAlignment="Stretch" ResizeBehavior="CurrentAndNext" />
        <TextBlock Grid.Column="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Right side</TextBlock>
    </Grid>

    <Grid Grid.Row="1" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button Grid.Column="0" Grid.ColumnSpan="3">Button 1</Button>
    </Grid>

    <Grid Grid.Row="2" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button Grid.Column="0">Button 2</Button>
        <Button Grid.Column="2">Button 3</Button>
    </Grid>


</Grid>

然而,这方面还需要做更多的事情,因为如果用户将 gridsplitter 拖到最大化到屏幕大小的表单的边缘,则无法检索它!
看起来 MS 的策略从来不是触摸网格中的行/列/单元格,因为它们基本上是用户定义的标准。

于 2017-12-31T10:14:33.203 回答