WPF - 具有三列的 GridSplitter
我有一个带有 3 列网格的应用程序。第一列和第二列之间的网格分离器工作得很好。为了让分离器位于第二列和第三列之间,我为分离器制作了一列。 (所以现在第三列实际上是第四列。)
当我调整大小时,其他列也会缩小。我认为这是因为我将它们设置为相对大小。但我不知道如何解决它。
这是我的问题的 XAML Pad Ready 示例。将其插入 XAML pad,然后尝试将最后一列的大小调整得更小。
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<StackPanel Background="#feca00" Grid.Column="0">
<TextBlock FontSize="35" Foreground="#58290A"
TextWrapping="Wrap">Left Hand Side</TextBlock>
</StackPanel>
<GridSplitter Width="10" />
<Border CornerRadius="10" BorderBrush="#58290A"
BorderThickness="5" Grid.Column="1">
<TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
TextWrapping="Wrap">Right Hand Side</TextBlock>
</Border>
<GridSplitter Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5"></GridSplitter>
<TabControl Grid.Column="3" Name="tabControl1">
<TabItem Header="Add Links" Name="tabAddLinks">
<Grid></Grid>
</TabItem>
</TabControl>
</Grid>
</Page>
感谢您的帮助!
编辑: 有人建议将两个分离器放在各自的列中可能会解决这个问题。我尝试过,现在第一个分离器也像第二个分离器一样缩小列。
以下是该示例的 XAML Pad 代码:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<StackPanel Background="#feca00" Grid.Column="0">
<TextBlock FontSize="35" Foreground="#58290A"
TextWrapping="Wrap">Left Hand Side</TextBlock>
</StackPanel>
<GridSplitter Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5"></GridSplitter>
<Border CornerRadius="10" BorderBrush="#58290A"
BorderThickness="5" Grid.Column="2">
<TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
TextWrapping="Wrap">Right Hand Side</TextBlock>
</Border>
<GridSplitter Grid.Column="3" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5"></GridSplitter>
<TabControl Grid.Column="4" Name="tabControl1">
<TabItem Header="Add Links" Name="tabAddLinks">
<Grid></Grid>
</TabItem>
</TabControl>
</Grid>
</Page>
I have an app with grid with 3 columns. The grid splitter between the first and second columns works just fine. To get the splitter over to be between the second and third columns I made a column for the splitter. (So now the the third column is really the fourth.)
When I resize the other columns also shrink. I assume that is because I have them set to be relative sized. But I don't know how to fix it.
Here is a XAML Pad Ready example of my issue. Plug this into XAML pad and then try to resize the last column to be smaller.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<StackPanel Background="#feca00" Grid.Column="0">
<TextBlock FontSize="35" Foreground="#58290A"
TextWrapping="Wrap">Left Hand Side</TextBlock>
</StackPanel>
<GridSplitter Width="10" />
<Border CornerRadius="10" BorderBrush="#58290A"
BorderThickness="5" Grid.Column="1">
<TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
TextWrapping="Wrap">Right Hand Side</TextBlock>
</Border>
<GridSplitter Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5"></GridSplitter>
<TabControl Grid.Column="3" Name="tabControl1">
<TabItem Header="Add Links" Name="tabAddLinks">
<Grid></Grid>
</TabItem>
</TabControl>
</Grid>
</Page>
Thanks for the help!
EDIT:
It was suggested that having both splitters in their own columns might fix it. I tried that and now the first splitter also shrinks the columns like the second splitter does.
Here is the XAML Pad code for that example:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<StackPanel Background="#feca00" Grid.Column="0">
<TextBlock FontSize="35" Foreground="#58290A"
TextWrapping="Wrap">Left Hand Side</TextBlock>
</StackPanel>
<GridSplitter Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5"></GridSplitter>
<Border CornerRadius="10" BorderBrush="#58290A"
BorderThickness="5" Grid.Column="2">
<TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
TextWrapping="Wrap">Right Hand Side</TextBlock>
</Border>
<GridSplitter Grid.Column="3" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5"></GridSplitter>
<TabControl Grid.Column="4" Name="tabControl1">
<TabItem Header="Add Links" Name="tabAddLinks">
<Grid></Grid>
</TabItem>
</TabControl>
</Grid>
</Page>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试为两个分离器设置
HorizontalAlignment="Center"
- 不知道为什么将其设置为“Right”会导致行为变得如此扭曲,但更改它对我有用:)Try setting
HorizontalAlignment="Center"
for both splitters - no idea why having it set to "Right" should cause the behaviour to go so screwy, but changing it worked for me :)GridSplitter 应放置在网格中其自己的列中。我不确定我是否完全理解您的问题,但我建议您尝试创建一个具有 5 个 ColumnDefinitions 的网格。使用第 1 列和第 2 列放置 GridSplitter,使用第 0、2 和 4 列放置内容。
GridSplitter MSDN 文档有一个关于如何使用的示例来做到这一点。
A GridSplitter should be placed within its own Column in a Grid. I'm not sure I understand your issue entirely, but I suggest you try creating a Grid with 5 ColumnDefinitions. Use columns 1 and 2 to place the GridSplitters and columns 0, 2 and 4 for content.
The GridSplitter MSDN doc has a sample on how to do this.
我也遇到了类似的问题,在调整大小时,三列拆分器会意外折叠。事实证明,这个问题与负边距有关(我这样做是为了布局目的),但显然它混淆了系统,因此从网格分割器中删除负边距对我有用。
I had a similar issue with 3-columns splitters collapsing unexpectedly at some point while resizing. it turned out the issue was related to negative
Margin
(I did that for layout purposes) but apparently it confuses the system, so removing the negative margin from the grid splitters worked for me.