這應該工作得很好。一個最小的娛樂沒有任何問題,工作對我來說:
視圖模型:
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的相關部分,它可能會有所幫助。
您可以嘗試在輸出窗口中查看是否有通知的綁定錯誤。我懷疑這裏應該有一些綁定錯誤(與直接設置的ListView或從父視覺繼承的實際DataContext有關)。 – 2014-11-21 15:54:46
@KingKing我在Binding的每個級別都創建了一個ShowCompact屬性,但這個屬性沒有被選中。 – Dabblernl 2014-11-22 06:07:33