使Listview(GridView)中的columheader动态折叠

时间:2014-11-21 15:03:26

标签: wpf xaml listview styles

我想使用ListViewGridView中展示数据。根据显示的数据量,我想让columheaders崩溃或可见。 我试图这样做:

   <Style TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Focusable" Value="False"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ShowCompact}" Value="True">
                <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

但这不起作用。怎么办呢?

1 个答案:

答案 0 :(得分:1)

这应该可以正常工作。最小的娱乐活动对我没有任何问题:

Screenshot 1 Screenshot 2

查看型号:

public class ListWindowViewModel : INotifyPropertyChanged
{
    private bool _showCompact;

    public bool ShowCompact
    {
        get { return _showCompact; }
        set
        {
            if (value == _showCompact)
                return;

            _showCompact = value;

            this.OnPropertyChanged("ShowCompact");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

查看:

<Window x:Class="StackOverflow.ListWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:l="clr-namespace:StackOverflow">
  <Window.DataContext>
    <l:ListWindowViewModel />
  </Window.DataContext>
  <Window.Resources>
    <Style TargetType="GridViewColumnHeader">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ShowCompact}" Value="True">
          <Setter Property="Visibility" Value="Collapsed" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
  <DockPanel LastChildFill="True">
    <CheckBox DockPanel.Dock="Top"
              Content="Show Compact"
              IsChecked="{Binding Path=ShowCompact, Mode=TwoWay}" />
    <ListView>
      <ListView.ItemsSource>
        <x:Array Type="s:String">
          <s:String>Item 1</s:String>
          <s:String>Item 2</s:String>
          <s:String>Item 3</s:String>
        </x:Array>
      </ListView.ItemsSource>
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Text" DisplayMemberBinding="{Binding .}" />
          <GridViewColumn Header="Length" DisplayMemberBinding="{Binding Length}" />
        </GridView>
      </ListView.View>
    </ListView>
  </DockPanel>
</Window>

仔细检查您是否绑定了公共财产并提出必要的更改通知事件。如果您发布视图模型和Xaml的相关部分,可能会有所帮助。