Search code examples
c#wpflistviewvisibilitygridviewcolumn

C#/WPF: Make a GridViewColumn Visible=false?


Is there an option to hide a GridViewColumn somehow like this:

<ListView.View>
    <GridView>
        <GridViewColumn Header="Test" IsVisible="{Binding Path=ColumnIsVisible}" />
    </GridView>
<ListView.View>

Edit: For clarity

Unfortunately, there is no "IsVisible" Property. I'm looking for a way to create that.

Edit: The solution based on the feedback looks like:

<GridViewColumn DisplayMemberBinding="{Binding Path=OptionColumn1Text}" 
                Width="{Binding Path=SelectedEntitiy.OptionColumn1Width}">
    <GridViewColumnHeader Content="{Binding Path=SelectedEntitiy.OptionColumn1Header}" IsEnabled="{Binding Path=SelectedEntitiy.OptionColumn1Width, Converter={StaticResource widthToBool}}" />
</GridViewColumn>

public class WidthToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value > 0;
    }

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

Solution

  • Edit: Reflecting the modified question.

    What about creating a 0 width column? Write a Boolean to Width IValueConverter, that takes a ColumnIsVisible as the ConverterParmeter?

     public class BooleanToWidthConverter : IValueConverter {
            public object Convert(object value, Type targetType, 
                                  object parameter, CultureInfo culture){
                return ((bool) parameter)? value : 0;
            }
     
            public object ConvertBack(object value, Type targetType, 
                                      object parameter, CultureInfo culture){
                throw new NotImplementedException();
            }
        }
    

    Something like:

    <ListView .. >
     <ListView.Resources>
      <BooleanToWidthConverter x:Key="boolToWidth" />
     </ListView.Resources>
    
     <ListView.View>
        <GridView>
            <GridViewColumn 
                      Header="Test" 
                      Width=
          "{Binding Path=ColumnWidth, 
                    Converter={StaticResource boolToWidth}, 
                    ConverterParameter=ColumnIsVisible}" />
        </GridView>
     <ListView.View>