2014-11-21 45 views
0

我想使用GridViewListView中显示数据。根据显示的数据量,我想让柱头折叠或可见。 我试图由此acomplish此:在列表视图(GridView)中创建柱头动态崩溃

<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> 

但这不工作。如何做呢?

+0

您可以尝试在输出窗口中查看是否有通知的绑定错误。我怀疑这里应该有一些绑定错误(与直接设置的ListView或从父视觉继承的实际DataContext有关)。 – 2014-11-21 15:54:46

+0

@KingKing我在Binding的每个级别都创建了一个ShowCompact属性,但这个属性没有被选中。 – Dabblernl 2014-11-22 06:07:33

回答

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的相关部分,它可能会有所帮助。

+0

感谢您花时间证明“这只是正常工作”。我再次检查绑定,发现我已经在错误的级别上实现了ShowCompact属性。 – Dabblernl 2014-11-29 21:31:30