简体   繁体   English

多个自动列定义导致奇怪的GridSplitter行为

[英]Multiple Auto column definitions causing weird GridSplitter behavior

I have the following XAML: 我有以下XAML:

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="0,0" Grid.Column="0" Background="Yellow" />
        <TextBlock Text="1,0" Grid.Column="1" Background="SkyBlue" />
        <GridSplitter Width="20" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Stretch" ShowsPreview="True" />
        <TextBlock Text="3,0" Grid.Column="3" Grid.Row="0" />
    </Grid>
</Window>

When there is only one ColumnDefinition with a Width of Auto , the GridSplitter works properly. 当只有一个ColumnDefinitionWidthAutoGridSplitter正常工作。 However, once there are multiple columns with a Width of Auto , the first Auto column also gets resized when the GridSplitter is moved (can be seen in cell 1,0 ). 但是,一旦有多个WidthAuto的列,第一个Auto列也会在移动GridSplitter时调整大小(可在单元格1,0看到)。

Before resize: 在调整大小之前:

在此输入图像描述

After resize: 调整大小后:

在此输入图像描述

How can I prevent the GridSplitter from resizing the second column? 如何防止GridSplitter调整第二列的大小?

If you cannot modify the existing columns, perhaps you could try to achieve this behavior programmatically... 如果您无法修改现有列,也许您可​​以尝试以编程方式实现此行为...

Name the columns: 命名列:

<Grid.ColumnDefinitions>
    <ColumnDefinition Name="firstColumn" Width="*" />
    <ColumnDefinition Name="secondColumn" Width="Auto" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

Add events to grid spliiter: 将事件添加到网格spliiter:

<GridSplitter Name="gridSplitter" DragStarted="gridSplitter_DragStarted" DragCompleted="gridSplitter_DragCompleted" />

Then just save the column widths before splitter drag and apply the change to the first column width, instead of second: 然后只需在拆分器拖动之前保存列宽,并将更改应用于第一列宽度,而不是第二列:

public partial class MainWindow : Window
{
    private double savedFirstColumnWidth;
    private double savedSecondColumnWidth;

    private void gridSplitter_DragStarted(object sender, DragStartedEventArgs e)
    {
        // Save the initial column width values
        savedFirstColumnWidth = firstColumn.ActualWidth;
        savedSecondColumnWidth = secondColumn.ActualWidth;
    }

    private void gridSplitter_DragCompleted(object sender, DragCompletedEventArgs e)
    {           
        double dragChange = e.HorizontalChange;

        // Change the width of the first column instead of second
        firstColumn.Width = new GridLength(savedFirstColumnWidth + dragChange);
        // Set the with of the second column to the value saved before the drag
        secondColumn.Width = new GridLength(savedSecondColumnWidth);
    }
}

I would modify layout to make sure that GridSplitter is working with adjacent grid columns: 我会修改布局以确保GridSplitter正在使用相邻的网格列:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="0,0" Grid.Column="0" Background="Yellow" />
        <TextBlock Text="1,0" Name="Txt" Grid.Column="1" Background="SkyBlue" />
    </Grid>

    <GridSplitter Width="20" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" ShowsPreview="True" />
    <TextBlock Text="3,0" Grid.Column="2" Grid.Row="0" />
</Grid>

I have been doing some testing, and I believe that I can explain what is happening, even if I am not entirely sure how to fix it. 我一直在做一些测试,我相信我可以解释发生了什么,即使我不完全确定如何解决它。

The test: Move the splitter around and watch the relative sizes of (0,0) and (3,0). 测试:移动分离器并观察(0,0)和(3,0)的相对大小。 They are always exactly equal. 他们总是完全平等。

Conclusion: This behavior is a result of the fact that both (0,0) and (3,0) are * width, so each of them has half the available width. 结论:这种行为是因为(0,0)和(3,0)都是* width,所以每个都有可用宽度的一半。 However, the splitter places a limit on the size of (3,0). 但是,分离器对(3,0)的大小设置了限制。 Since (3,0) has a limit to its size, but also is supposed to have half the available width, this means that (0,0) also has the same size limits as (3,0). 由于(3,0)对其大小有限制,但也应该具有可用宽度的一半,这意味着(0,0)也具有与(3,0)相同的大小限制。 As the splitter forces (3,0) to shrink, (0,0) must also shrink to maintain the proportion specified by the * width. 当分离器力(3,0)收缩时,(0,0)也必须收缩以保持*宽度指定的比例。 As a result (1,0) and (2,0) are the only columns that are allowed to grow to fill the remaining space, and since (2,0) only contains the splitter with a static width of 20, (1,0) grows to fill the remaining space. 结果(1,0)和(2,0)是唯一允许增长以填充剩余空间的列,并且因为(2,0)仅包含静态宽度为20的分割器,(1, 0)增长以填补剩余空间。

As I said, I am not sure how to fix this, but the first step to fixing a problem is understanding it. 正如我所说,我不知道如何解决这个问题,但解决问题的第一步是理解它。 So hopefully this will help someone discover the solution. 所以希望这有助于有人发现解决方案。

EDIT: Further testing indicates that the above is only true if (1,0) is set to Auto or Fixed Width. 编辑:进一步测试表明,只有当(1,0)设置为自动或固定宽度时,上述情况才为真。 If (1,0) is set to * or some multiple of *, then all of the * sections seem to behave oddly, and grow and shrink with no regard to their supposed proportions. 如果(1,0)设置为*或*的某个倍数,那么所有*部分似乎都表现得很奇怪,并且在不考虑它们的假设比例的情况下增长和缩小。

EDIT2: Looking around I came accross this link: https://wpf.2000things.com/tag/gridsplitter/ 编辑2:环顾四周我来到这个链接: https//wpf.2000things.com/tag/gridsplitter/

One of the things that is mentioned at that link is the following: 该链接中提到的一件事情如下:

Recall that a GridSplitter in its own column and with its HorizontalAlignment set to Center will resize columns on either side of it. 回想一下,GridSplitter在其自己的列中并且其Horizo​​ntalAlignment设置为Center将调整其两侧的列的大小。 In the example below, columns 0 and 2 are resized, but the width of column 3 is not changed. 在下面的示例中,列0和2的大小调整,但列3的宽度不会更改。

All of the observed behavior in my tests fits in with one of the examples mentioned at that link, so my new conclusion is that this behavior is all intentional, rather then a strange bug like I supposed all along. 在我的测试中观察到的所有行为都符合该链接中提到的一个示例,所以我的新结论是这种行为都是有意的,而不是像我一直认为的那样奇怪的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM