.net 如何防止 WPF GridSplitter 改变我的网格大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/279557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I keep the WPF GridSplitter from changing the size of my Grid?
提问by Zack Peterson
WPF GridSplitter makes my Grid wider than my Window!
WPF GridSplitter 使我的网格比我的窗口更宽!
I've got a WPF Grid with a GridSplitter. If I resize my columns, then I can make my grid wider than my window and non-viewable.
我有一个带有 GridSplitter 的 WPF 网格。如果我调整列的大小,那么我可以使我的网格比我的窗口更宽并且不可见。
It starts like this:
它是这样开始的:
WPF Grid http://img201.imageshack.us/img201/9505/onehg6.jpg
WPF 网格 http://img201.imageshack.us/img201/9505/onehg6.jpg
But after widening the left column, I can no longer see the right column (green):
但是在左栏加宽后,我再也看不到右栏(绿色):
WPF GridSplitter http://img201.imageshack.us/img201/1804/twomy6.jpg
WPF GridSplitter http://img201.imageshack.us/img201/1804/twomy6.jpg
What am I doing wrong? How do I keep the GridSplitter from changing the size of my Grid?
我究竟做错了什么?如何防止 GridSplitter 改变我的网格大小?
Update:
更新:
I'm still struggling with this. I've now tried nesting grids within grids. That didn't help. Here's my XAML ColumnDefinitions, RowDefinitions, and GridSplitters...
我仍在为此而苦苦挣扎。我现在尝试在网格中嵌套网格。那没有帮助。这是我的 XAML ColumnDefinitions、RowDefinitions 和 GridSplitters...
<Window ... >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="150" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="400" />
</Grid.ColumnDefinitions>
<GridSplitter
ResizeDirection="Columns"
ResizeBehavior="BasedOnAlignment"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Width="2"
Margin="0,5,0,5"
Panel.ZIndex="1"/>
<Grid Grid.Column="0">
...
</Grid>
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="150" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="200" />
</Grid.ColumnDefinitions>
<GridSplitter
ResizeDirection="Columns"
ResizeBehavior="PreviousAndNext"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Width="2"
Margin="0,5,0,5"
Panel.ZIndex="1"/>
<Grid Grid.Column="0">
...
</Grid>
<Grid Grid.Column="2">
...
</Grid>
</Grid>
</Grid>
</Window>
Update:
更新:
I think the problem is with the WebBrowser control. See new question:
我认为问题出在 WebBrowser 控件上。见新问题:
采纳答案by Robert Macnee
If your Window is resized so its Width is less than the sum of your columns' MinWidths, you'll see the columns cut off, but otherwise I can't reproduce your problem:
如果您的 Window 被调整大小使其宽度小于列的 MinWidths 的总和,您将看到列被切断,但否则我无法重现您的问题:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="150" Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="400" Width="*"/>
</Grid.ColumnDefinitions>
<GridSplitter
Width="2"
Grid.Column="1"
HorizontalAlignment="Center"
Margin="0,5,0,5"
Panel.ZIndex="1"
VerticalAlignment="Stretch"
ResizeBehavior="BasedOnAlignment"
ResizeDirection="Columns"/>
<Grid Grid.Column="0">
<Border Background="Red" Margin="5"/>
</Grid>
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="150" Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="200" Width="*"/>
</Grid.ColumnDefinitions>
<GridSplitter
Width="2"
Grid.Column="1"
HorizontalAlignment="Center"
Margin="0,5,0,5"
Panel.ZIndex="1"
VerticalAlignment="Stretch"
ResizeBehavior="PreviousAndNext"
ResizeDirection="Columns"/>
<Grid Grid.Column="0">
<Border Background="Green" Margin="5"/>
</Grid>
<Grid Grid.Column="2">
<Border Background="Blue" Margin="5"/>
</Grid>
</Grid>
</Grid>
</Window>
Expanding the red column, it will only expand until the right column reaches its MinWidth of 400, it won't boot it off the page.
扩展红色列,它只会扩展到右列达到其 MinWidth 400,它不会将其从页面引导。
It's possible you're setting other properties of the Window or the outermost Grid that would cause this behavior...
您可能正在设置会导致此行为的 Window 或最外层 Grid 的其他属性...
回答by Abe Heidebrecht
Try changing your Width's to star sizes. This will cause the splitter to only resize the columns between which it sits, so not sure if this is your desired behavior. However, with star sizes, the content will not grow beyond the bounds of the window.
尝试将您的宽度更改为星号大小。这将导致拆分器仅调整其所在列的大小,因此不确定这是否是您想要的行为。但是,对于星号大小,内容不会超出窗口范围。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" MinWidth="100" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="50" />
<ColumnDefinition Width="2*" MinWidth="100" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="3*" MinWidth="150" />
</Grid.ColumnDefinitions>
<GridSplitter
ResizeDirection="Columns"
Grid.Column="1"
Grid.RowSpan="8"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Width="2"
Margin="0,5,0,5"
Panel.ZIndex="1"/>
...
</Grid>
回答by user958933
It works for me without any additional code when there are no Columns with Auto Width between the splitters, i.e.:
当拆分器之间没有具有自动宽度的列时,它无需任何附加代码即可对我来说有效,即:
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" MinWidth="50" MaxWidth="500" />
<ColumnDefinition Width="Auto"/> <!-- Remove such columns /-->
<ColumnDefinition Width="100*" MinWidth="850"/>
<ColumnDefinition Width="30*" MinWidth="50" MaxWidth="800" />
</Grid.ColumnDefinitions>
...
<GridSplitter HorizontalAlignment="Right" Width="3"/>
...
<GridSplitter Grid.Column="3" HorizontalAlignment="Left" Width="3" />
<!-- Assign Grid.Column to 2 if you remove the auto width column /-->
...
</Grid>
Otherwise the grid will be resizable.
否则网格将调整大小。
回答by Nestor
Capturing the DragDelta event is another way of doing it:
捕获 DragDelta 事件是另一种方法:
private void VerticalGridSplitter_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
if (GridName.ColumnDefinitions[2].Width.Value < 400)
{
GridName.ColumnDefinitions[2].Width = new GridLength(400);
}
}
But using MinWidth on a * ColumnDefinition should work just fine. Note that the ColumnDefinition with the MinWidth needs to be at the top level. It doesnt work if it's nested in some grid inside a column.
但是在 * ColumnDefinition 上使用 MinWidth 应该可以正常工作。请注意,具有 MinWidth 的 ColumnDefinition 需要位于顶层。如果它嵌套在列内的某个网格中,则它不起作用。
回答by Jan Willem B
I had this problem too, with a normal Gridwith no special controls, and *size on both columns/rows.
我也有这个问题,Grid没有特殊控件的正常,以及*列/行的大小。
The problem turned out to be that I programmatically set the width an height of columns/rows in the grid after loading the Window, because I save the splitter position to restore it on the next program run.
问题原来是我在加载窗口后以编程方式设置网格中列/行的宽度和高度,因为我保存了拆分器位置以在下一次程序运行时恢复它。
I changed it to calculating a *size to solve the problem.
我将其更改为计算*大小以解决问题。

