WPF Control TabItem from checkbox

I looked: Binding visibility using DependencyProperty

and 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

I have some tabs that I want to control their visibility from the checkbox i.e..

<TabItem Header="Preferences" Name="tabItem4"></TabItem> 

Ideally, I would do

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

or some, but this is not the correct syntax. What is simple / correct syntax?

+7
source share
1 answer

You can use the built-in BooleanToVisibilityConverter . Here's a working sample:

 <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> 
+35
source

All Articles