将绑定分配给附加属性以隐藏 GridViewColumn

Assigning Binding to Attached Properties to hide a GridViewColumn

在 WPF 中,使用 MVVM 模型,我正在努力解决我认为是绑定问题。我有一个 ListView 旨在显示一组对象。 ItemsSource 绑定到集合,每个 GridViewColumn 用于分隔该对象的属性。此视图(UserControl)在主 window 上的 TabControl 中的多个选项卡中通用。每个选项卡都需要隐藏某些列(逻辑发生在 ViewModel)。

我创建了一个行为 class 来附加 DependecyProperties 用于几件事,包括 属性 IsHidden 用于 GridViewColumn。考虑到这一点,这里有一个示例设置:

行为类 - 最小化示例

public static class LayoutColumn
{
  public static readonly DependencyProperty HiddenProperty = DependencyProperty.RegisterAttached(
      "IsHidden",
      typeof(bool),
      typeof(LayoutColumn));

  public static bool GetIsHidden(DependencyObject obj)
  {
    return (bool)obj.GetValue(HiddenProperty);
  }

  public static void SetIsHidden(DependencyObject obj, bool isHidden)
  {
    obj.SetValue(HiddenProperty, isHidden);
  }

  public static bool IsHidden(this GridViewColumn column)
  {
    bool? isHidden = column.GetProperty<bool>(HiddenProperty);

    // Debug
    string format = "{0}.IsHidden = {1}";
    if (isHidden.HasValue)
    {
      Console.WriteLine(format, column.Header, isHidden.Value);
    }
    else
    {
      Console.WriteLine(format, column.Header, "Null");
    }

    return isHidden.HasValue && isHidden.Value;
  }

  private static T? GetProperty<T>(this GridViewColumn column, DependencyProperty dp) where T : struct
  {
    if (column == null)
    {
      throw new ArgumentNullException("column");
    }

    object value = column.ReadLocalValue(dp);

    if (value != null && value.GetType() == dp.PropertyType)
    {
      return (T)value;
    }

    return null;
  }
} // end LayoutColumn class

public class LayoutManager
{
  // Methods and logic to enforce column min/max width, hidden, fixed, etc by attaching to the ListView and GridViewColumn event handlers.
}

示例对象class

public class Example
{
  public string Foo { get; set; }
  public string Bar { get; set; }
  public string Baz { get; set; }
}

ViewModel

public class MyDisplayViewModel : INotifyPropertyChanged
{
  private bool hidden;
  private ObservableCollection<Example> examples;

  ...

  public bool HideColumn
  {
    get 
    {
      return this.hidden;
    }

    set 
    {
      this.hidden = value;
      this.OnPropertyChanged("HideColumn");
    }
  }

  public ObservableCollection<Example> ExampleCollection
  {
    get 
    {
      return this.examples;
    }

    set 
    {
      this.examples = value;
      this.OnPropertyChanged("ExampleCollection");
    }
  }
}

一些XAML

<ListView
        Name="LogListView"
        ItemsSource="{Binding ExampleCollection}"
        ListViewBehaviors:LayoutManager.Enabled="{Binding AttachProperty}">
        <ListView.View>
            <GridView>
                <GridViewColumn
                    Width="Auto"
                    ListViewBehaviors:LayoutColumn.MinWidth="40"
                    ListViewBehaviors:LayoutColumn.MaxWidth="200"
                    ListViewBehaviors:LayoutColumn.IsHidden="True"
                    DisplayMemberBinding="{Binding Foo, Mode=OneWay}"
                    Header="Hidden Column"/>
                <GridViewColumn
                    Width="Auto"
                    ListViewBehaviors:LayoutColumn.MinWidth="74"
                    ListViewBehaviors:LayoutColumn.MaxWidth="200"
                    ListViewBehaviors:LayoutColumn.IsHidden="False"
                    DisplayMemberBinding="{Binding Bar, Mode=OneWay}"
                    Header="Visible Column"/>
                <GridViewColumn
                    Width="Auto"
                    ListViewBehaviors:LayoutColumn.IsFixed="True"
                    ListViewBehaviors:LayoutColumn.IsHidden="{Binding Path=DataContext.HideColumn, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}},
                        Mode=OneWay}"
                    DisplayMemberBinding="{Binding Baz, Mode=OneWay}"
                    Header="Dynamic Visibility">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

在此处演示,手动将附加的 属性 IsHidden 设置为 true 或 false 可以正确地 hide/show 关联的列。当 IsHidden 为 null(该列未设置附加行为)时,预期的行为是该列显示正常。

问题

绑定列总是默认显示,对我来说这意味着IsHidden没有被设置。我觉得我很接近,但我所有的研究结果都是在做我在这里所做的事情的例子。我是 WPF 的新手,所以我不知道还要搜索什么。

编辑

我尝试了以下方法,但都失败了:

AncestorType={x:Type ListView}
AncestorType={x:Type UserControl}
AncestorType={x:Type Window}

我需要做什么才能将GridViewColumn附加属性绑定到ViewModel HideColumn 属性?

正如@RohitVats 所指出的,主要错误在于我假设隐藏该列的代码运行良好。我依靠 LayoutManager 来验证最小和最大宽度 - 这(在 LayoutColumn class 中)将 return 它们的值(如果列被隐藏,则调整为 0),然后会调整大小专栏。这个想法可行,但需要更多。正如@Ganesh 所建议的那样,我需要解决这个问题的大部分内容来自 here

清洁工XAML

通过大量 Console.WriteLine 和更多的调试,我发现我过度复杂化了绑定。

ListViewBehaviors:LayoutColumn.IsHidden="{Binding HideColumn}"

行为的补充Class

XAML 中的更改只有在我对 LayoutColumn class 设置进行了一些更改后才能生效,方法是将 PropertyMetadata 添加到 HiddenProperty 并且另一个 DependencyProperty 用于保存之前隐藏的宽度:

public static readonly DependencyProperty HiddenProperty = DependencyProperty.RegisterAttached(
  "IsHidden",
  typeof(bool),
  typeof(LayoutColumn)
  new PropertyMetadata(false, OnHiddenChanged));

public static readonly DependencyProperty VisibleWidthProperty = DependencyProperty.RegisterAttached(
  "VisibleWidth",
  typeof(double),
  typeof(LayoutColumn),
  new UIPropertyMetadata(double.NaN));

public static double GetVisibleWidth(DependencyObject obj)
{
  return (double)obj.GetValue(VisibleWidthProperty);
}

public static void SetVisibleWidth(DependencyObject obj, double value)
{
  obj.SetValue(VisibleWidthProperty, value);
}

private static void OnHiddenChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
  GridViewColumn column = dependencyObject as GridViewColumn;

  if (column != null)
  {
    if (e.Property == HiddenProperty)
    {
      bool hide = (bool)e.NewValue;

      if (hide)
      {
        SetVisibleWidth(column, column.Width);
        column.Width = 0;
      }
      else
      {
        column.Width = GetVisibleWidth(column);
      }
    }
  }
}

就是这样!我没有使用 link 中所示的 Converter,因为我在绑定的两端处理 bool 类型,但它同样有效。希望这对其他人有所帮助。