这应该工作得很好。一个最小的娱乐没有任何问题,工作对我来说:
视图模型:
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