WPF 不需要的网格分割器行为
我有一个包含 3 列的简单网格(其中一列包含网格分割器)。当调整网格大小并且左列达到其最小宽度时,它不会不执行任何操作,而是会增加右列的宽度。谁能帮我阻止这一切吗?
我无法设置右列的最大宽度,因为网格本身也会调整大小。
这是一些显示问题的示例代码。调整大小时,将鼠标移到红色区域上:
XAML:
<Grid DockPanel.Dock="Top" Height="200">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200" Width="*" />
<ColumnDefinition Width="3" />
<ColumnDefinition MinWidth="120" Width="240" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="0" />
<DockPanel LastChildFill="True" Grid.Row="0" Grid.Column="2" >
<Rectangle DockPanel.Dock="Right" Width="20" Fill="Blue" />
<Rectangle Fill="Green" />
</DockPanel>
<GridSplitter Background="LightGray" Grid.Row="0" Grid.Column="1" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
I have a simple grid with 3 columns (one of which contains a grid splitter). When resizing the grid and the left column reaches its minimum width, instead of doing nothing it increases the width of the right column. Could anyone help me stop this?
I can't set the max width of the right column, because the grid itself also resizes.
Here's some sample code that shows the problem. While resizing, move the mouse over the red area:
XAML:
<Grid DockPanel.Dock="Top" Height="200">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200" Width="*" />
<ColumnDefinition Width="3" />
<ColumnDefinition MinWidth="120" Width="240" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="0" />
<DockPanel LastChildFill="True" Grid.Row="0" Grid.Column="2" >
<Rectangle DockPanel.Dock="Right" Width="20" Fill="Blue" />
<Rectangle Fill="Green" />
</DockPanel>
<GridSplitter Background="LightGray" Grid.Row="0" Grid.Column="1" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这有点晚了,但我刚刚遇到了这个问题,这是我的解决方案。不幸的是它不够通用,它只适用于具有两列的网格,但它可能可以进一步适应。然而,它解决了所描述的问题和我自己的问题,所以这里是:
解决方案包括黑客或解决方法,无论你想如何称呼它。您不必为左列和右列声明 MinWidth,而是为第一列声明 MinWidth 和 MaxWidth。这意味着 GridSplitter 不会移动到定义位置的右侧。到目前为止,一切都很好。
下一个问题是,如果我们有一个可调整大小的容器(在我的例子中是窗口),这还不够。这意味着我们无法将左列放大到我们想要的程度,即使第二列可能有足够的空间。幸运的是,有一个解决方案:绑定 Grid ActualWidth 并使用加法转换器。转换器参数实际上是右列所需的 MinWidth,显然是负值,因为我们需要从网格宽度中减去它。您也可以使用 SubtractConvertor,但这取决于您。
这是 xaml 和代码:
以及转换器:
我希望这有帮助,
Mihai Drebot
I know this is a bit late, but I just run into this problem, and here is my solution. Unfortunately it is not general enough, it only works for a grid with two columns, but it can probably be adapted farther. However, it solves the described problem and my own, so here goes:
The solution consists in a hack or workaround, however you want to call it. Instead of declaring MinWidth for both the left and right column, you declare a MinWidth and a MaxWidth for the first column. This means that the GridSplitter won't move right of a defined location. So far, so good.
The next problem is that if we have a resizable container (the window in my case), this is not enough. It means that we cannot enlarge the left column as much as we want, even though there might be plenty of space for the second one. Fortunately, there is a solution: binding on the Grid ActualWidth and using an addition converter. The converter parameter will actually be the desired MinWidth for the right column, obviously the negative value, since we need to subtract it from the Grid Width. You could also use a SubtractConvertor, but that is up to you.
Here goes the xaml and code:
and the converter:
I hope this helps,
Mihai Drebot
这种方法是一种 hack,但可以达到预期的效果。尝试在 Window.SizeChanged 事件上放置一个事件处理程序以设置第一个 columndef 的 maxWidth。例如:
我需要使用此方法来防止网格顶行中的第三方控件由于 gridSplitter 忽略宽度设置为“自动”的列的最小/最大宽度而产生一些不需要的换行。 borderBuffer 可能需要根据您的情况进行调整。在我的例子中,基于几何/列/边框宽度, borderBuffer 并没有完全有意义 - 它只是适用于我的布局的神奇数字。
如果有人能提出更清洁的解决方案,我很乐意使用它。这个解决方案让我想起了痛苦地尝试强制 VB6 调整控件大小 - 恶心。但就目前而言,它比网格顶行上的项目由于意外的包装而被隐藏要好。
This approach is a hack but can be used for the desired effect. Try putting an event handler on the Window.SizeChanged event to set the maxWidth of the first columndef. For example:
I needed to use this method to prevent a third party control in the top row of the grid from having some undesirable wrapping resulting from when the gridSplitter disregards the min/max width of the column with width set to "Auto". The borderBuffer may need to be adjusted for your case. The borderBuffer in my case didn't make perfect sense based on the geometry/column/border widths - it was just the magic number that worked for my layout.
If someone can come up with a cleaner solution, I'd love to use it instead. This solution reminds me of painfully trying to force VB6 to resize controls - yuck. But for now, it beats having items on the top row of my grid from getting hidden due to unexpected wrapping.
我通过将列定义中的
*
更改为Auto
并将240
更改为*
来阻止这种不良行为。I got this undesirable behaviour to stop by changing the
*
in the columndefinition toAuto
and the240
to*
.