Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf data binding grid row visibility

I have a window with a grid acting like a form. The window isn't my own and there is a new requirement to not show (ie, collapse) rows 4 and 5 based on a user selected context.

The two things I can think of to make this work is to either:

  1. Have a converter on the row content which takes a bool and collapses the visibility if true.
  2. Have a converter on the grid row height property.

I prefer the latter, but am at a loss to get the input value for the converter. The converter code and binding is below.

Can someone tell me what the binding should look like to make this work? Is there some easier way to do this?

Converter Code

[ValueConversion(typeof(GridLength), typeof(Visibility))]
public class GridLengthToCollapseVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (value == null || parameter == null) return Binding.DoNothing;

        var result = (GridLength) value;
        bool shouldCollapse;
        Boolean.TryParse(parameter.ToString(), out shouldCollapse);
        return shouldCollapse ? new GridLength() : result;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}

The Binding (this is where I am stuck)

Say I want the value of the height to be 30 unless the bound ShowLastName property is true. The binding below isn't right, but what is?

 <RowDefinition Height="{Binding Source=30, Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter=ShowLastName}" />

Working Solution

[ValueConversion(typeof(bool), typeof(GridLength))]
public class GridLengthToCollapseVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (value == null || parameter == null) return Binding.DoNothing;

        bool shouldCollapse;
        Boolean.TryParse(value.ToString(), out shouldCollapse);
        return shouldCollapse 
            ? new GridLength(0) 
            : (GridLength) parameter;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }

}

    <Grid.Resources>
        <cvt:GridLengthToCollapseVisibilityConverter x:Key="GridLengthToCollapseVisibilityConv" />
        <GridLength x:Key="AutoSize">Auto</GridLength>
        <GridLength x:Key="ErrorLineSize">30</GridLength>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{StaticResource AutoSize}" />
        <RowDefinition Height="{StaticResource ErrorLineSize}" />
        <RowDefinition Height="{Binding Path=HideLastName, 
            Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter={StaticResource AutoSize}}" />
        <RowDefinition Height="{Binding Path=HideLastName, 
            Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter={StaticResource ErrorLineSize}}" />
    </Grid.RowDefinitions>
like image 976
Berryl Avatar asked May 21 '26 12:05

Berryl


1 Answers

You can't databind the ConverterParamater: http://social.msdn.microsoft.com/Forums/en/wpf/thread/88a22766-5e6f-4a16-98a6-1ab39877dd09

Why not switch the value and parameter if the height always is the same:

<RowDefinition Height="{Binding Source=ShowLastName, Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter=30}" />

If you need to databind both values you could use multi-value bindings: http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx

like image 82
Richard L Avatar answered May 24 '26 17:05

Richard L