MVVM动态设置GridViewColumn宽度

时间:2017-03-27 20:19:26

标签: c# wpf listview mvvm column-width

在我的mvvm应用程序中,我有一个listview。 我喜欢显示/隐藏一些列 listview,取决于复选框“显示所有列”的状态(此处:Col1应显示/隐藏)。

这是我非常简化的代码。怎么了?! 显然这不起作用!

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

2 个答案:

答案 0 :(得分:0)

这就是你所需要的......

public bool ShowAllColumns
{
    get { return _bShowAllColumns; }
    set
    {
        if (_bShowAllColumns != value)
        {
            _bShowAllColumns = value;
            OnPropertyChanged("ShowAllColumns");
        }
    }
}

答案 1 :(得分:0)

GridViewColumn未添加到可视树中,并且不会继承任何DataContext,因此您无法在不使用{Width的情况下将其BindingProxy属性绑定到视图模型的源属性{1}}这里建议的课程:https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/