本文介绍了来自复选框的 WPF 控制 TabItem 可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看了:使用 DependencyProperty 的可见性绑定
和http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspxhttp://www.vistax64.com/avalon/240-no-checkbox-checkedchanged.htmlhttp://geekswithblogs.net/thibbard/archive/2007/12/10/wpf---showhide-element-based-on-checkbox.checked.aspx
我有一些标签,我想通过复选框控制它们的可见性即
I have some tabs I want to control their visibility from a checkboxi.e.
<TabItem Header="Preferences" Name="tabItem4"></TabItem>
理想情况下我会这样做
<TabItem Header="Preferences" Name="tabItem4">
<DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True">
<Setter Property="Visibility" Value="True" />
</DataTrigger>
</TabItem>
或一些类似但不正确的语法.什么是最简单/正确的语法?
or some such but that is not correct syntax. What is easiest/correct syntax?
推荐答案
您可以使用内置的 BooleanToVisibilityConverter.这是一个工作示例:
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>
这篇关于来自复选框的 WPF 控制 TabItem 可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!