首页 > 解决方案 > BooleanToVisibilityConverter 折叠/显示 TabItem

问题描述

MainWindow.xaml我有以下...

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
</Window.Resources>

// Tab control

<TabItem x:Name="DebugTab" Header="Debug" Visibility="{Binding Path=DebugTabState, Converter={StaticResource ResourceKey=BooleanToVisibility}}">
    // Some content
</TabItem>

MainWindow.xaml.cs我有以下...

public bool DebugTabState
{
    get
    {
        return AppData.EnableDebuggingCheckBox;
    }
}

DebugSettings.xaml我有以下...

<CheckBox x:Name="EnableDebuggingCheckBox" Content="Enable Debugging" IsChecked="{Binding Path=EnableDebugging}" />

DebugSettings.xaml.cs我有以下...

public bool EnableDebugging
{
    get
    {
        return AppData.EnableDebuggingCheckBox;
    }
    set
    {
        AppData.EnableDebuggingCheckBox = value;
    }
}

最后,在AppData.cs我有以下...

private bool _enableDebuggingCheckBox;

public bool EnableDebuggingCheckBox
{
    get
    {
        return _enableDebuggingCheckBox;
    }
    set
    {
        _enableDebuggingCheckBox = value;
        OnPropertyChanged("EnableDebuggingCheckBox");
    }
}

选中和取消选中 EnableDebuggingCheckBox 会按预期将值更新为 true 或 false,但 DebugTab 没有隐藏或显示。我错过了什么吗?

谢谢!

标签: c#wpf

解决方案


如果 Appdata 是 MainWindow 中的公共属性,请将绑定更改为:

<TabItem x:Name="DebugTab" Header="Debug" Visibility="{Binding Path=AppData.EnableDebuggingCheckBox, Converter={StaticResource ResourceKey=BooleanToVisibility}}">
    // Some content
</TabItem>

然后,您可以删除 MainWindow.DebugTabState。

或者,在 MainWindow 中建议从 AppData 中的 INotifyPropertyChange 并在 EnableDebuggingCheckBox 更改时引发 PropertyChanged 事件


推荐阅读