WPF GridSplitter after resizing

Once the grating grid is used to resize the grid, row * will not recover space when other rows are collapsed.

I have the following grid in the main view with three rows. The data grid is at the top of the splitter in the middle and the contentcontrol is on the last line. The splitter has a closed button to collapse parts. All this works with the exception that after resizing the user using gridsplitter grid.

<Grid Margin="3,0"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Style="{StaticResource CollapsableRow}"/><!-- Splitter Here --> <RowDefinition Style="{StaticResource CollapsableRow}"/> </Grid.RowDefinitions> 

GridSplitter Style:

  <Style x:Key="gridSplitterStyle" TargetType="{x:Type GridSplitter}"> <Setter Property="Visibility" Value="{Binding IsItemSelected, Converter={StaticResource BoolToShow},ConverterParameter='Visible|Collapsed'}" /> <Setter Property="Width" Value="Auto"/> <Setter Property="Height" Value="14"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="Border.BorderBrush" Value="#FF6593CF" /> <Setter Property="Border.BorderThickness" Value="0,1,0,0" /> <Setter Property="UIElement.SnapsToDevicePixels" Value="True" /> <Setter Property="UIElement.Focusable" Value="False" /> <Setter Property="Control.Padding" Value="7,7,7,7" /> <Setter Property="Cursor" Value="SizeNS" /></Style> 

As I said, the crash works correctly if the gridsplitter grid is used to resize. After that, gaps remain.

EDIT: HB and Kononi had simple and consistent sentences, so I tried to implement them without success in the data trigger:

 <Style x:Key="CollapsableRow" TargetType="{x:Type RowDefinition}"> <Style.Triggers> <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="True"> <Setter Property="RowDefinition.Height" Value="0"/> </DataTrigger> <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="False"> <Setter Property="RowDefinition.Height" Value="Auto"/> </DataTrigger> </Style.Triggers> </Style> 
+6
source share
4 answers

Since the grid splitter and details were already hidden, Visibility was the obvious choice to reset the height of the next line definition.

  /// <summary> /// Grid splitter that show or hides the following row when the visibility of the splitter is changed. /// </summary> public class HidableGridSplitter : GridSplitter { GridLength height; public HidableGridSplitter() { this.IsVisibleChanged += HideableGridSplitter_IsVisibleChanged; this.Initialized += HideableGridSplitter_Initialized; } void HideableGridSplitter_Initialized(object sender, EventArgs e) { //Cache the initial RowDefinition height, //so it is not always assumed to be "Auto" Grid parent = base.Parent as Grid; if (parent == null) return; int rowIndex = Grid.GetRow(this); if (rowIndex + 1 >= parent.RowDefinitions.Count) return; var lastRow = parent.RowDefinitions[rowIndex + 1]; height = lastRow.Height; } void HideableGridSplitter_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { Grid parent = base.Parent as Grid; if (parent == null) return; int rowIndex = Grid.GetRow(this); if (rowIndex + 1 >= parent.RowDefinitions.Count) return; var lastRow = parent.RowDefinitions[rowIndex + 1]; if (this.Visibility == Visibility.Visible) { lastRow.Height = height; } else { height = lastRow.Height; lastRow.Height = new GridLength(0); } } 
+6
source

You can use animation to solve overriding row / column definitions with gridsplitter. See My answer to a similar question in GridSplitter overrides ColumnDefinition style trigger?

+2
source

If you use GridSplitter, Heights is no longer Auto , but specific values. You need to manually change the values โ€‹โ€‹back using the style or events and the code behind, for example. this resets the double-click auto-size column:

 private void ColumnSplitter_DoubleClick(object sender, MouseButtonEventArgs e) { if (!ColumnTreeView.Width.IsAuto) ColumnTreeView.Width = new GridLength(); } 
+1
source

Based on what you provide, GridSplitter will resize the previous and next rows. You can see this in action with this code:

 <Grid Margin="3,0"> <Grid.RowDefinitions> <RowDefinition x:Name="row0" Height="*" /> <RowDefinition Height="Auto" /> <RowDefinition x:Name="row2" Height="Auto" /> </Grid.RowDefinitions> <Border Background="Red" > <TextBlock Text="{Binding ElementName=row0, Path=Height}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> <GridSplitter Grid.Row="1" Style="{StaticResource gridSplitterStyle}" HorizontalAlignment="Stretch" /> <Border Background="Blue" Grid.Row="2" MinHeight="50"> <TextBlock Text="{Binding ElementName=row2, Path=Height}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </Grid> 

The last row size will actually change from Auto to a fixed height. Therefore, even if you destroy the contents in this line, it will still occupy the specified space. You will need to reset the line to Height="Auto" to really collapse it with its contents.

0
source

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


All Articles