问题描述
我有一个3列的网格:第一个网格的宽度设置为*,我一直认为它会填充剩下的其他列留下的空间。第二个宽度为8,第三个宽度设置为自动,因此其大小根据其内容而变化。
在第二列中,我有一个 奇怪的是,如果我不使用 XAML Code(condensed)按要求: GridSplitter
,所以拖动时我可以改变第一列和第三列的宽度。这工作正常,问题是我的第三列中有一个网格,当切换时,它的可见性设置为折叠状态。当崩溃时,我需要第一列来填充所有剩余的空间。我尝试了很多方法:
HorizontalAlignment
设置为 Stretch
Grid.Rowspan
绑定到第三个,所以当隐藏时,Rowspan将变为3,并且由于它的宽度是使用*,所以它理论上应该使用所有3列中的所有可用空间。
ul>
GridSplitter
调整列的大小,那么第一列将填满所有剩余空间。然而,在调整大小之后,第一列不会改变。这几乎就像,当拖动GridSplitter来调整列的大小时,WPF会将两列的宽度更改为绝对值,而不是它们的星号和自动值,这样它们就不会在调整大小后填充空格。
< Grid Grid.Row = 1\" >
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
< Grid x:Name =AssetListViewGridGrid.Column =0Grid.RowSpan ={Binding Visibility,ElementName = AssetViewMetadataSplitter,Converter = {StaticResource SplitterVisibilityToRowSpanConverter}}Margin =0 4 0 4 >
<! - 不相关的代码 - >
< / Grid>
< GridSplitter x:Name =AssetViewMetadataSplitterGrid.Column =1Opacity =0.8HorizontalAlignment =CenterWidth =6Margin =3 5 1 5ToolTip =Grab to resize Visibility ={Binding IsChecked,ElementName = GridHeaderVisibilityToggleButton,Converter = {StaticResource VisConverter}}/>
< Grid x:Name =MetadataGridMargin =4 2 4 2Grid.Column =2DataContext ={Binding MetadataViewModel}Visibility ={Binding IsChecked,ElementName = GridHeaderVisibilityToggleButton,Converter = {StaticResource VisConverter}}>
<! - 不相关的代码 - >
< / Grid>
我也遇到了这个问题。在我的应用程序中,似乎GridSplitter实际上将宽度值更改为绝对值,而不是*,所以我猜这就是行为。
我结束了使用代码隐藏来解决它。首先,name列1:
< ColumnDefinition Width =*x:Name =Column1/>
然后,为您的MetadataGrid可见性更改时添加一个事件处理程序,&调用此代码将Column1重置为*宽度以填充其余部分:
Column1.Width = new GridLength(1,GridUnitType。星);
您也可以尝试使用样式触发器在XAML中执行某些操作,将Column1宽度重置为* MetadataGrid的可见性状态。我的情况比自定义尺寸&各种条件和条件扩展器,所以我使用了代码隐藏功能,所以我不确定xaml触发器是否适用于yoU。希望这会有所帮助。
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.
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:
- Set
HorizontalAlignment
on first column toStretch
- 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.
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 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>
I just ran into this as well. In my app it seemed that a GridSplitter was in fact changing the width values to absolute instead of * also, so I'm guessing that is the behavior.
I ended up using code-behind to solve it. First, name column 1:
<ColumnDefinition Width="*" x:Name="Column1"/>
Then, add an event handler for when your MetadataGrid visibility changes, & call this code to reset Column1 to a * width to fill the rest:
Column1.Width = new GridLength(1, GridUnitType.Star);
You might also try doing something in the XAML with style triggers to reset the Column1 width to * based on the MetadataGrid's visibility state. My case was much more complex with custom sizes & varied conditions & expanders, so I used code-behind, so I'm not sure if xaml triggers would work for yoU or not . Hopefully that helps though.
这篇关于wpf - 当其他列的内容被折叠时,网格列不会填充剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!