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

Joseph jun. Melettukunnel picture Joseph jun. Melettukunnel · Sep 8, 2009 · Viewed 33.9k times · Source

Does anyone know if there is an option to hide a GridViewColumn somehow like this:

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

Thanks a lot!

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();
    }
}

Thanks to all!
Cheers

Answer

Preet Sangha picture Preet Sangha · Sep 8, 2009

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>