Search code examples
c#wpflistviewmvvmcolumn-width

MVVM Setting GridViewColumn width dynamically


In my mvvm application I have a listview. I like to show/hide some columns of the listview, depending on the state of the checkbox "Show all columns" (Here: Col1 should be showed/hidden).

That's my very simplified code. What is wrong?! Obviously this does't work!

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="185">

  <Window.Resources>
    <local:ConverterHideListViewColumn x:Key="ConverterHideListViewColumn" />
  </Window.Resources>

  <Grid>
    <ListView Height="100" Width="100">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Col0" Width="40"/>
          <GridViewColumn Header="Col1" Width="{Binding ShowAllColumns, Converter={StaticResource ConverterHideListViewColumn}}"/>
        </GridView>
      </ListView.View>
    </ListView>

    <CheckBox 
      Content="Show all columns"
      IsChecked="{Binding ShowAllColumns, Mode=TwoWay}"
      Margin="40,140,0,0">
    </CheckBox>

  </Grid>
</Window>

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    VM _vm;

    public MainWindow ()
    {
      InitializeComponent ();
      _vm = new VM ();
      this.DataContext = _vm;
    }
  }

  /// <summary>
  /// Dummy Viewmodel
  /// </summary>
  public class VM : INotifyPropertyChanged
  {
    private bool _bShowAllColumns;
    public event PropertyChangedEventHandler PropertyChanged;

    public VM ()
    {
      ShowAllColumns = true;
    }

    public bool ShowAllColumns
    {
      get { return _bShowAllColumns; }
      set { _bShowAllColumns = value; }
    }

    private void OnPropertyChanged (string propertyName)
    {
      var handler = PropertyChanged;
      if (handler != null)
        handler (this, new PropertyChangedEventArgs (propertyName));
    }
  }


  /// <summary>
  /// Converter for setting the ListView-Column width, depending on value VM.ShowAllColumns
  /// </summary>
  class ConverterHideListViewColumn : IValueConverter
  {
    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      bool   bShowAllColumns = false;
      double dWidth          = 0;

      if (value is bool)
      {
        bShowAllColumns = (bool) value;
        dWidth = bShowAllColumns? 40 : 0;
      }

      return dWidth;
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotSupportedException ();
    }
   }
}

Solution

  • A GridViewColumn is not added to the visual tree and doesn't inherit any DataContext so you cannot bind its Width property to a source property of a view model without using a BindingProxy class as suggested here: https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/