wpf - 当其他列的内容被折叠时,网格列不填充剩余空间 [英] wpf - Grid column not filling remaining space when other column's contents are collapsed

查看:14
本文介绍了wpf - 当其他列的内容被折叠时,网格列不填充剩余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 列的网格:第一列的 Width 设置为*",我被引导相信它会填满其他列留下的任何剩余空间.第二个的宽度为 8,第三个的 Width 设置为Auto",因此其大小会根据其内容而变化.

I've got a grid with 3 columns: the first has its Width set to "*", which I have been led to believe will make it fill up any remaining space left by the other columns. The second has a width of 8, and the third's Width is set to "Auto" so its size changes depending on its contents.

在我的第二列中,我有一个 GridSplitter,因此当拖动时我可以更改第一列和第三列的宽度.这很好用,问题是我的第三列中有一个网格,当切换时,它的可见性设置为折叠.折叠时,我需要第一列来填充所有剩余空间.我尝试了很多方法:

In my 2nd column I have a GridSplitter, so that when dragged I can change the width of both the first and third columns. This works fine, the issue is that I have a grid in my third column that, when toggled, will have its visibility set to collapsed. When collapsed, I need the first column to fill all of the remaining space. I tried to do this many ways:

  • 将第一列上的 Horizo​​ntalAlignment 设置为 Stretch
  • 将第一列的 Grid.Rowspan 绑定到第三列的可见性,这样当隐藏时,Rowspan 将变为 3,并且由于其宽度使用*",它理论上应该使用所有 3 列中的所有可用空间.
  • Set HorizontalAlignment on first column to Stretch
  • Bound the Grid.Rowspan of the first column to the visibility of the third one, so that when when hidden the Rowspan will change to 3 and, since its width is using "*", it should theoretically use all of the available space in all 3 columns.

奇怪的是,如果我不使用 GridSplitter 调整列的大小,那么第一列将正确填充所有剩余空间.然而在调整大小之后,第一列不会让步.几乎就好像,当拖动 GridSplitter 来调整列的大小时,WPF 将两列的宽度更改为绝对而不是它们的星号和自动值,使其在调整大小后不会填充空间.

The weird thing is that, if I do not resize the columns using the GridSplitter, then the first column will fill all remaining space properly. Yet after resizing, the first column will not budge. It's almost as if, when dragging the GridSplitter to resize the columns, WPF change the width of both columns to become absolute instead of their star and auto values, making it so they will not fill the space after a resize.

XAML 代码(压缩)按要求:

XAML Code (condensed) as requested:

<Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
    <Grid x:Name="AssetListViewGrid" Grid.Column="0" Grid.RowSpan="{Binding Visibility, ElementName=AssetViewMetadataSplitter, Converter={StaticResource SplitterVisibilityToRowSpanConverter}}"  Margin="0 4 0 4">
    <!-- irrelevant code -->
    </Grid>
<GridSplitter x:Name="AssetViewMetadataSplitter" Grid.Column="1" Opacity="0.8" HorizontalAlignment="Center" Width="6" Margin="3 5 1 5" ToolTip="Grab to resize" Visibility="{Binding IsChecked, ElementName=GridHeaderVisibilityToggleButton, Converter={StaticResource VisConverter}}"/>
<Grid x:Name="MetadataGrid" Margin="4 2 4 2" Grid.Column="2" DataContext="{Binding MetadataViewModel}" Visibility="{Binding IsChecked, ElementName=GridHeaderVisibilityToggleButton, Converter={StaticResource VisConverter}}">
    <!-- irrelevant code -->
</Grid>

推荐答案

这是一个有点旧的帖子,但万一其他人在这里,我通过在里面添加空的 10000X1000 rect 来解决它

This is a bit old post but in case anyone else gets here, I solved it by adding empty 10000X1000 rect inside it

[<Grid Grid.Row=1";Grid.Column=1"高度=自动"Horizo​​ntalAlignment =左"SizeChanged=Grid_SizeChanged"><图像 x:名称=当前图像";高度=自动"宽度=自动"/><矩形宽度=10000"高度=10000"></矩形><Canvas x:Name="currentImageOverLay"/>]

这篇关于wpf - 当其他列的内容被折叠时,网格列不填充剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆