Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF converter on grid column and row length only works if gridsplitter is not used

In WPF I have a boolean to length converter that I have binded to column and row definitions.

This converter is used to read a binded boolean in order to determine whether the row/column should be hidden or shown.

This is done so I can "maximize" a given portion of a grid or return it back to its original size. This works so long as I don't use the gridsplitter to resize. As soon as this happens it no longer sets the desired length. I have a feeling once the gridsplitter is used it removes the binding on the column definition. Is there a work around 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>

Edit: Restructured question to include more XAML

Edit: Additional info, when I wish to expand a grid section I set all booleans to false and set the section I want to true. For example, if I wish to maximize Grid.Column 2 and Grid.Row 2 I set all the booleans to false except ShowColumn2 and ShowRow2. This works as intended and when the booleans are all set to true, to get the original view back it, it works as intended as well. The issue is once I use a gridsplitter to resize a column it doesn't work as before. When I maximize the same section as before it works fine, but when I attempt to resize back to the original size Grid.Column 2 and Grid.Row 2 takes up the whole bottom row. The two columns besides them are minimized.

This leads me to believe that when the gridsplitter is used the converter will no longer be able to set the column's value to true.

like image 459
David Lee Avatar asked Mar 16 '17 22:03

David Lee


1 Answers

In your ColumnDefinition you need to set the MaxWidth to zero to hide the Column and MaxHeight to zero for Row.

try the following converter:

[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;
    }
}

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

Also consider to remove TwoWay-Binding.

like image 121
WPFGermany Avatar answered Oct 30 '22 10:10

WPFGermany