2014-11-21 47 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