MVVM 动态设置 GridViewColumn 宽度

MVVM Setting GridViewColumn width dynamically

在我的 mvvm 应用程序中,我有一个列表视图。 我喜欢 show/hide 的某些专栏 列表视图,取决于复选框的状态 "Show all columns"(这里:Col1 应该是 showed/hidden)。

这是我非常简化的代码。怎么了?! 显然这是行不通的!

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

这就是您所需要的...

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

A​​ GridViewColumn 未添加到可视化树中并且不继承任何 DataContext 因此您无法将其 Width 属性 绑定到源 属性 不使用 BindingProxy class 的视图模型,如此处所建议:https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/