Wpf - a grid column that does not fill the remaining space when the contents of another column collapse

I have a grid with three columns: the first has a Width equal to "*", which, in my opinion, will make it fill the remaining space left by the other columns. The second has a width of 8, and the third is Auto, so its size changes depending on its contents.

I have in my second column GridSplitter, so when dragging and dropping I can change the width of both the first and third columns. This works well, the problem is that I have a grid in my third column, which, when switched, will have its visibility, which will be minimized. When it crashes, I need the first column to fill all the remaining space. I tried to do it differently:

  • Set HorizontalAlignmentin the first column toStretch
  • Associate the Grid.Rowspanfirst column with the visibility of the third, so that when it is hidden, the Rowspan will change to 3 and, since its width uses "*", theoretically it should use all available space in all three columns.

The strange thing is that if I do not resize the columns with GridSplitter, then the first column will correctly fill all the remaining space. But after resizing, the first column will not budge. It is almost as if while dragging and dropping the GridSplitter to resize columns, WPF changed the width of both columns to become absolute, not their star and auto values, so they won’t fill the space after resizing.

XAML code (with seal) on request:

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

+4
source share
1 answer

I also came across this. In my application, it seemed that GridSplitter actually changes the width values ​​to absolute, and not to *, so I assume this behavior.

I decided to use the code to solve it. First enter column 1:

<ColumnDefinition Width="*" x:Name="Column1"/>

, MetadataGrid, reset Column1 *, :

Column1.Width = new GridLength(1, GridUnitType.Star);

- XAML reset 1 * MetadataGrid. , , , xaml yoU . , .

0

Source: https://habr.com/ru/post/1598241/


All Articles