wpf gridsplitter问题 - 双方缩小

时间:2023-01-15 17:34:26

Gridsplitter trouble:

I have a: grid with 4 columns

我有一个:4列的网格

  • Column 0 contains a grid with 3 columns
  • 第0列包含一个包含3列的网格

  • Column 1 contains a gridsplitter
  • 第1列包含一个网格分析器

  • Column 2 contains a stackpanel of width 20 pixels
  • 第2列包含宽度为20像素的堆叠面板

  • Column 3 contains a grid with 3 columns
  • 第3列包含一个包含3列的网格

When the gridsplitter is moved to either the left or right, BOTH panels shrink the same amount -- one should shrink and the other should grow.

当网格分割器向左或向右移动时,两个面板收缩相同的数量 - 一个应缩小,另一个应该增长。

I am hesitant to include the xaml, but you are going to ask for it, so here is an abridged version. I have only removed a few unrelated controls, and gutted the treeviews and listviews. If you really need the whole thing, then of course I can supply it.

我对包含xaml犹豫不决,但你会要求它,所以这里是一个删节版本。我只删除了一些不相关的控件,并删除了treeviews和listviews。如果你真的需要整个东西,那么我当然可以提供它。

Thanks for any help!

谢谢你的帮助!

<Window x:Class="Calvin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="Calvin" >
    <DockPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" LastChildFill="True" >
        <Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.497" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="5"  />
                <ColumnDefinition Width="20"  />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Width="2*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"  TextWrapping="NoWrap" Text="Field One"  HorizontalAlignment="Stretch"/>
                <TextBox Grid.Row="0" Grid.Column="4" Grid.ColumnSpan="3"  TextWrapping="NoWrap" Text="Field Two"  HorizontalAlignment="Stretch" />
                <TreeView DockPanel.Dock="Left" Grid.Column="0" Grid.Row="1" Name="PaneOneTree" 
                          Width="Auto"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                </TreeView>
                <GridSplitter Grid.Column="1" Grid.Row="1" Width="5" HorizontalAlignment="Center"/>
                <ScrollViewer Grid.Column="2" Grid.Row="1" >
                    <ListView DockPanel.Dock="Left" Name="FileDetailsLeft" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name" Width="120"  />
                                <GridViewColumn Header="Size" Width="120"  />
                                <GridViewColumn Header="Access Time" Width="120"  />
                                <GridViewColumn Header="Extension" Width="120"  />
                            </GridView>
                        </ListView.View>
                    </ListView>
                </ScrollViewer>
            </Grid>
            <GridSplitter Grid.Column="1" Grid.Row="0" Width="5" HorizontalAlignment="Left" />
            <StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="Auto" Background="Red">
                <Button  >Move</Button>
                <Button >Other</Button>
            </StackPanel>
            <Grid Grid.Column="3" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"  TextWrapping="NoWrap" Background="Cornsilk"
                         Text="Pane Two Text"  HorizontalAlignment="Stretch"/>
                <TextBox Grid.Row="0" Grid.Column="4" Grid.ColumnSpan="3"  TextWrapping="NoWrap"
                         Text="Pane Two Text"  HorizontalAlignment="Stretch" Background="Linen" />
                <TreeView DockPanel.Dock="Left" Grid.Column="0" Grid.Row="1" Name="PaneTwoTree" Width="Auto"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                          Background="SeaShell" >
                </TreeView>
                <GridSplitter Grid.Column="1" Grid.Row="1" Width="5" HorizontalAlignment="Center" />
                <ScrollViewer Grid.Column="2" Grid.Row="1" >
                    <ListView DockPanel.Dock="Left" Name="FileDetailsRight" 
                              HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Background="Moccasin" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name" Width="120"  />
                                <GridViewColumn Header="Size" Width="120"  />
                                <GridViewColumn Header="Access Time" Width="120"  />
                                <GridViewColumn Header="Extension" Width="120"  />
                            </GridView>
                        </ListView.View>
                        <ListViewItem >List Item</ListViewItem>
                        <ListViewItem >List Item</ListViewItem>
                        <ListViewItem >List Item</ListViewItem>
                        <ListViewItem >List Item</ListViewItem>
                        <ListViewItem >List Item</ListViewItem>
                        <ListViewItem >List Item</ListViewItem>
                        <ListViewItem >List Item</ListViewItem>
                    </ListView>
                </ScrollViewer>
            </Grid>
        </Grid>
    </DockPanel>
</Window>

And, of course, feel free to suggest better ways to do anything:) -- I'm still learning.

而且,当然,随意提出更好的方法来做任何事情:) - 我还在学习。

:bp:

2 个解决方案

#1


0  

Ok, I see your problem now... basically, you can't set an exact Width on a column who's Width will be changed by a GridSplitter control. Instead, you can only set MinWidth and/or MaxWidth property on the ColumnDefinition, but be aware that you can then not use the "*" notation. Also, looking at the code example below, you can see that you could have stripped out a whole load more code for your question example... this was all that was required to demonstrate your problem (before I fixed it):

好的,我现在看到你的问题...基本上,你不能在一个列上设置一个确切的宽度,其宽度将由GridSplitter控件改变。相反,您只能在ColumnDefinition上设置MinWidth和/或MaxWidth属性,但请注意,您不能使用“*”表示法。另外,查看下面的代码示例,您可以看到您可能已经为您的问题示例删除了更多的代码...这就是演示您的问题所需的全部内容(在我修复它之前):

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" MinWidth="150" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="20" />
        <ColumnDefinition Width="*" MinWidth="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Rectangle Grid.Column="0" Fill="PowderBlue" />
    <GridSplitter Grid.Column="1" Background="Black" HorizontalAlignment="Stretch" />
    <Rectangle Grid.Column="2" Fill="Red" Height="100" />
    <Rectangle Grid.Column="3" Fill="Purple" />
</Grid>

#2


21  

I had the same issue. When I moved one GridSplitter it would also move the other (Usually in the opposite direction.). I eventually discovered that I had forgotten to set a property of the GridSplitter. For a GridSplitter you MUST have both vertical and horizontal properties set ie...

我遇到过同样的问题。当我移动一个GridSplitter时,它也会移动另一个(通常是相反的方向)。我最终发现我忘了设置GridSplitter的属性。对于GridSplitter,你必须设置垂直和水平属性,即......

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <GridSplitter Grid.Column="1" Width="2" 
        VerticalAlignment="Stretch" HorizontalAlignment="Center"/>
</Grid>

#1


0  

Ok, I see your problem now... basically, you can't set an exact Width on a column who's Width will be changed by a GridSplitter control. Instead, you can only set MinWidth and/or MaxWidth property on the ColumnDefinition, but be aware that you can then not use the "*" notation. Also, looking at the code example below, you can see that you could have stripped out a whole load more code for your question example... this was all that was required to demonstrate your problem (before I fixed it):

好的,我现在看到你的问题...基本上,你不能在一个列上设置一个确切的宽度,其宽度将由GridSplitter控件改变。相反,您只能在ColumnDefinition上设置MinWidth和/或MaxWidth属性,但请注意,您不能使用“*”表示法。另外,查看下面的代码示例,您可以看到您可能已经为您的问题示例删除了更多的代码...这就是演示您的问题所需的全部内容(在我修复它之前):

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" MinWidth="150" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="20" />
        <ColumnDefinition Width="*" MinWidth="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Rectangle Grid.Column="0" Fill="PowderBlue" />
    <GridSplitter Grid.Column="1" Background="Black" HorizontalAlignment="Stretch" />
    <Rectangle Grid.Column="2" Fill="Red" Height="100" />
    <Rectangle Grid.Column="3" Fill="Purple" />
</Grid>

#2


21  

I had the same issue. When I moved one GridSplitter it would also move the other (Usually in the opposite direction.). I eventually discovered that I had forgotten to set a property of the GridSplitter. For a GridSplitter you MUST have both vertical and horizontal properties set ie...

我遇到过同样的问题。当我移动一个GridSplitter时,它也会移动另一个(通常是相反的方向)。我最终发现我忘了设置GridSplitter的属性。对于GridSplitter,你必须设置垂直和水平属性,即......

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <GridSplitter Grid.Column="1" Width="2" 
        VerticalAlignment="Stretch" HorizontalAlignment="Center"/>
</Grid>