Wpf grid row visibility

I have a window with a grid acting like a shape. The window is not mine, and there is a new requirement not to show (i.e. Collapse) lines 4 and 5 based on the context selected by the user.

Two things I can think of to do this job are either:

  • Have a converter for the contents of the string, which takes the value bool and resets visibility if true.
  • The converter has a grid line height property.

I prefer the latter, but I cannot get the input value for the converter. Converter code and binding below.

Can someone tell me what the binding should look like to make this work? Is there an 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(); }
}

Binding (that's where I got stuck)

, , 30, ShowLastName . , ?

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

[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>
+3
2

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

, :

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

, : http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx

+2

, , .


, , MultiBinding, . , .

0

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


All Articles