WPF控制TabItem来自复选框的可见性

Cha*_*oll 8 wpf binding wpf-controls

我看了一下: 使用DependencyProperty进行可见性绑定

http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx http://www.vistax64.com/avalon/240-no-checkbox-checkedchanged.html http:/ /geekswithblogs.net/thibbard/archive/2007/12/10/wpf---showhide-element-based-on-checkbox.checked.aspx

我有一些标签我想通过复选框来控制它们的可见性,即

        <TabItem Header="Preferences" Name="tabItem4"></TabItem>
Run Code Online (Sandbox Code Playgroud)

理想情况下我会这样做

        <TabItem Header="Preferences" Name="tabItem4">
                <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True">
                    <Setter Property="Visibility" Value="True" />
                </DataTrigger>

         </TabItem>
Run Code Online (Sandbox Code Playgroud)

或者一些这样的但是语法不正确.什么是最简单/正确的语法?

Mat*_*ton 37

您可以使用内置的BooleanToVisibilityConverter.这是一个工作样本:

<Window x:Class="WpfApplication16.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:WpfApplication16"
            Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="b2v" />
    </Window.Resources>

    <StackPanel>
        <CheckBox x:Name="chk" Content="Show There" />
        <TabControl>
            <TabItem Header="Hello" />
            <TabItem Header="There" Visibility="{Binding IsChecked,ElementName=chk,Converter={StaticResource b2v}}" />
            <TabItem Header="World" />
        </TabControl>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)