WPF to grid column converter and row length only work if gridsplitter is not used

In WPF, I have a boolean to length converter that I have bound to the definitions of columns and rows.

This converter is used to read the bound logical value to determine whether the row / column should be hidden or shown.

This is done so that I can "maximize" a given part of the grid or return it back to its original size. This works until I use gridsplitter to resize. Once this happens, it no longer sets the desired length. I get the feeling that when gridsplitter is used, it removes the binding by column definition. Is there any work for this?

Converter

[ValueConversion(typeof(bool), typeof(GridLength))]
public class BoolToGridLengthConverter : IValueConverter
{
    public int Length { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value == true) ? new GridLength(Length == 0 ? 1 : Length, GridUnitType.Star) : new GridLength(0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {    // Don't need any convert back
        return null;
    }
}

Xaml

<Grid>
    <Grid.Resources>
        <helpers:BoolToGridLengthConverter x:Key="BoolToGridLengthConverter1" Length="1" />
        <helpers:BoolToGridLengthConverter x:Key="BoolToGridLengthConverter2" Length="2" />
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding ShowColumn1, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="{Binding ShowColumn2, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="{Binding ShowColumn3, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding ShowRow1, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
        <RowDefinition Height="5" />
        <RowDefinition Height="{Binding ShowRow2, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter2}}" />
    </Grid.RowDefinitions>
    <Grid Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="0"></Grid>
    <GridSplitter Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="1" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="5" />
    <Grid Grid.Column="0" Grid.Row="2"></Grid>
    <GridSplitter Grid.Column="1" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
    <Grid Grid.Column="2" Grid.Row="2"></Grid>
    <GridSplitter Grid.Column="3" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
    <Grid Grid.Column="4" Grid.Row="2"></Grid>
</Grid>

: XAML

: , , false , , true. , Grid.Column 2 Grid.Row 2, false, ShowColumn2 ShowRow2. , , true, , , . , gridsplitter , -. , , , Grid.Column 2 Grid.Row 2 . , , .

, grips, true.

+6
1

ColumnDefinition MaxWidth , Column MaxHeight Row.

:

[ValueConversion(typeof(bool), typeof(double))]
public class BoolToMaxConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value == true) ? double.MaxValue : 0d;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {    // Don't need any convert back
        return null;
    }
}

xaml:

<Grid>
    <Grid.Resources>
        <local:BoolToMaxConverter x:Key="BoolToMaxConverter" />
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MaxWidth="{Binding ShowColumn1, Converter={StaticResource BoolToMaxConverter}}"  />
        <ColumnDefinition Width="5" />
        <ColumnDefinition MaxWidth="{Binding ShowColumn2, Converter={StaticResource BoolToMaxConverter}}" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition MaxWidth="{Binding ShowColumn3, Converter={StaticResource BoolToMaxConverter}}" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition MaxHeight="{Binding ShowRow1, Converter={StaticResource BoolToMaxConverter}}" />
        <RowDefinition Height="5" />
        <RowDefinition  MaxHeight="{Binding ShowRow2, Converter={StaticResource BoolToMaxConverter}}" />
    </Grid.RowDefinitions>
    <Grid Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="0"></Grid>
    <GridSplitter Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="1" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="5" />
    <Grid Grid.Column="0" Grid.Row="2"></Grid>
    <GridSplitter Grid.Column="1" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
    <Grid Grid.Column="2" Grid.Row="2"></Grid>
    <GridSplitter Grid.Column="3" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
    <Grid Grid.Column="4" Grid.Row="2"></Grid>
</Grid>

TwoWay-Binding.

+1

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


All Articles