WPF - 具有三列的 GridSplitter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1894937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:19:34  来源:igfitidea点击:

WPF - GridSplitter with three columns

wpflayoutgridsplitter

提问by Vaccano

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.)

我有一个带有 3 列网格的应用程序。第一列和第二列之间的网格拆分器工作正常。为了让分流器位于第二列和第三列之间,我为分流器创建了一个列。(所以现在第三列实际上是第四列。)

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.

这是我的问题的 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> 

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:

这是该示例的 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> 

回答by kiwipom

Try setting HorizontalAlignment="Center"for both splitters - no idea whyhaving it set to "Right" should cause the behaviour to go so screwy, but changing it worked for me :)

尝试设置HorizontalAlignment="Center"两个分离器 - 不知道为什么将它设置为“正确”会导致行为变得如此糟糕,但改变它对我有用:)

回答by Szymon Rozga

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.

GridSplitter 应该放置在网格中它自己的列中。我不确定我是否完全理解您的问题,但我建议您尝试使用 5 个 ColumnDefinitions 创建一个网格。使用第 1 列和第 2 列放置 GridSplitter,使用第 0、2 和 4 列放置内容。

The GridSplitter MSDN dochas a sample on how to do this.

GridSplitter MSDN文档有如何做到这一点的样本。

<Grid.ColumnDefinitions>
  <ColumnDefinition/>
  <ColumnDefinition Width="Auto" />
  <ColumnDefinition/>
</Grid.ColumnDefinitions>
...
<GridSplitter Grid.Column="1"
          HorizontalAlignment="Center"
          VerticalAlignment="Stretch"
          Background="Black" 
          ShowsPreview="True"
          Width="5"
          />