简体   繁体   English

列宽=“自动”时WPF GridSplitter奇怪的行为

[英]WPF GridSplitter strange behavior when Column Width=“Auto”

I have pretty simple layout, look: 我有非常简单的布局,看起来:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" MinWidth="200"></ColumnDefinition>
        <ColumnDefinition Width="5"></ColumnDefinition>
        <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0"
            BorderBrush="Red"
            BorderThickness="2">
        <!-- Any picture-->
        <Image Source="/Resources/PreviewTest.png"></Image>
    </Border>
    <GridSplitter Grid.Column="1" 
                    Width="5"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Stretch"
                    ResizeBehavior="PreviousAndNext"></GridSplitter>
    <Expander Grid.Column="2"
                ExpandDirection="Left"
                BorderBrush="RoyalBlue"
                BorderThickness="2">
        <!-- Any picture-->
        <Image Source="/Resources/PreviewTest.png"></Image>    
    </Expander>
</Grid>

The problem: when I'm dragging GridSplitter to left, right column goes out from window border as shown on animation. 问题是:当我将GridSplitter拖动到左侧时,右侧列从窗口边框向外移动,如动画所示。 I found that it happens when width of third column is set as "Auto" ( Width="Auto" ). 我发现当第三列的宽度设置为“自动”( Width="Auto" )时会发生这种情况。 If I set Width="*" GridSplitter works fine and third Column doesn't go out from window border. 如果我设置Width="*" GridSplitter工作正常,第三Column不会从窗口边框出去。 So why when Width="Auto" it happens? 那么为什么当Width="Auto"时会发生?

示范

Your problem is caused by the fact that one of your ColumnDefinition s is set to Auto . 您的问题是由您的一个ColumnDefinition设置为Auto What is happening is that when the first column reaches its MinWidth value, it can't get any smaller. 发生的事情是,当第一列达到其MinWidth值时,它不会变得更小。 However, if you keep moving the GridSlitter , then the right column has to grow. 但是,如果继续移动GridSlitter ,则右列必须增长。 Seeing as you let it grow to any size, you get your current problem. 看到你让它成长为任何尺寸,你就会遇到当前的问题。

All that you need to do to fix it is to set the right ColumnDefinition.Width property to the * value as well. 要解决这个问题,您需要做的就是将正确的ColumnDefinition.Width属性设置为*值。 In this way, it can't grow out of the Grid any more. 通过这种方式,它不再能够从Grid生长出来。 If you need to, you can control the end sizes of the columns using the ColumnDefinition.MaxWidth property. 如果需要,可以使用ColumnDefinition.MaxWidth属性控制列的结束大小。 Try this: 试试这个:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" MinWidth="200" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="*" MinWidth="50" />
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" BorderBrush="Red" BorderThickness="2">
        <Image Source="/Resources/PreviewTest.png" />
    </Border>
    <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Center" VerticalAlignment="Stretch" ResizeBehavior="BasedOnAlignment" />
    <Expander Grid.Column="2" ExpandDirection="Left" BorderBrush="RoyalBlue" BorderThickness="2">
        <Image Source="/Resources/PreviewTest.png" />
    </Expander>
</Grid>

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

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