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

时间:2015-03-05 20:04:21

标签: c# wpf xaml listview mvvm

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

我创建了一个行为类来附加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.
}

示例对象类

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

视图模型

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可正常工作以隐藏/显示关联列。当IsHidden为null(列没有附加的行为集)时,预期的行为是列显示为正常。

问题

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

修改

我尝试了以下内容,但都失败了:

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

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

1 个答案:

答案 0 :(得分:0)

正如@RohitVats指出的那样,主要错误在于我假设隐藏列的代码播放得很好。我依靠LayoutManager来验证最小和最大宽度 - 这些(在LayoutColumn class中)将返回它们的值(如果列被隐藏则调整为0),反过来会调整列的大小。这个想法有效但需要更多。我需要解决这个问题的大部分来自here,正如@Ganesh所建议的那样。

清洁XAML

通过大量的Console.WriteLine调试,我发现我的绑定过于复杂。

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

添加行为类

一旦我对LayoutColumn课程设置进行了一些更改,只需将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); } } } } 以保存之前的版本,XAML中的更改才能生效隐藏宽度:

Converter

那就是它!我没有使用链接中显示的bool,因为我在绑定的两端处理{{1}}类型,但它也可以正常工作。希望这对其他人有所帮助。