Does anyone know if there is an option to hide a GridViewColumn somehow like this:
One simpler approach, that still uses the concept of setting the columns width to zero but does not have the side effects of using a IValueConverter
(the user can still drag the column wider) is to create a new getter/setter that returns a width based on your ColumnIsVisible
variable and then bind to that:
public double ColumnWidth
{
get
{
if (this.ColumnIsVisible)
{
return 100;
}
else
{
return 0;
}
}
set
{
OnPropertyChanged("ColumnWidth");
}
}
Make your bindings TwoWay and if the user attempts to drag the column wider OnPropertyChanged
will be called and reset the width to 0. You might have to use a binding proxy though for your binding. Also add a call to OnPropertyChanged("ColumnWidth")
when ever ColumnIsVisible is updated :)
Here is another solution based on setting the column's width to zero. I have modified it a little. It now works like this:
Here is the code.
XAML:
<GridViewColumn
HeaderTemplate="..."
HeaderContainerStyle="...">
<GridViewColumnHeader
Content="Header text"
Visibility="{Binding AppliesToColumnVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
behaviors:GridViewBehaviors.CollapseableColumn="True" />
BooleanToVisibilityConverter:
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(value.ToString());
if (param == true)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Attached behavior GridViewBehaviors.CollapseableColumn:
public static readonly DependencyProperty CollapseableColumnProperty =
DependencyProperty.RegisterAttached("CollapseableColumn", typeof(bool), typeof(GridViewBehaviors),
new UIPropertyMetadata(false, OnCollapseableColumnChanged));
public static bool GetCollapseableColumn(DependencyObject d)
{
return (bool)d.GetValue(CollapseableColumnProperty);
}
public static void SetCollapseableColumn(DependencyObject d, bool value)
{
d.SetValue(CollapseableColumnProperty, value);
}
private static void OnCollapseableColumnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
GridViewColumnHeader header = sender as GridViewColumnHeader;
if (header == null)
return;
header.IsVisibleChanged += new DependencyPropertyChangedEventHandler(AdjustWidth);
}
static void AdjustWidth(object sender, DependencyPropertyChangedEventArgs e)
{
GridViewColumnHeader header = sender as GridViewColumnHeader;
if (header == null)
return;
if (header.Visibility == Visibility.Collapsed)
header.Column.Width = 0;
else
header.Column.Width = double.NaN; // "Auto"
}
Use if Thumb.DragDelta may solve the problem
I use it in listview as
<ListView x:Name="MyListView"IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Items}", Mode=Default, Source={StaticResource DataProvider}}"
Thumb.DragDelta="Thumb_DragDelta">
public Window1()
{
InitializeComponent();
MyListView.AddHandler(Thumb.DragDeltaEvent, new DragDeltaEventHandler(Thumb_DragDelta), true );
void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Thumb senderAsThumb = e.OriginalSource as Thumb;
GridViewColumnHeader header = senderAsThumb.TemplatedParent as GridViewColumnHeader;
if (header.Column.ActualWidth < MIN_WIDTH)
{
header.Column.Width = MIN_WIDTH;
}
if (header.Column.ActualWidth > MAX_WIDTH)
{
header.Column.Width = MAX_WIDTH;
}
}
}
I've set the column the width="0"
.
Now the column looks like its not visible. But i do not know if it will affect anything else.
It might be a dummy solution but for now it works.
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>