我有一个TabControl在UserControl一个视图模型的支持,以及Visibility标签项目之一,必将对视图模型的属性.
<TabControl x:Name="myTabControl">
<TabItem Header="Tab 1" />
<TabItem Header="Tab 2" Visibility="{Binding HasData, Converter={StaticResource boolToVisibilityConverter}}"/>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
当Visibility的TabItem变化,它缩短(隐藏)的TabItem报头,但是它继续显示其内容.
我想TabControl,以切换到可视选项卡时,其他选项卡是隐藏的,并且有点惊讶地发现,它不会自动发生.
将事件处理程序附加到节目的SelectionChanged事件上TabControl,TabItem.IsSelected(和TabControl.SelectedItem)在TabItem.Visibility更改时甚至不受影响(这是一个错误吗?!).
我试过了两个属性触发器:
<!-- This doesn't compile because of TargetName on the Setter, think you can only use it in Control Templates.
I don't know how to refer to the parent TabControl from within the TabItem style. -->
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Collapsed">
<Setter TargetName="myTabControl" Property="SelectedIndex" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
</TabControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
和数据触发器:
<!-- This doesn't quite work, it affects the Visibility of the TabItem's content too -->
<TabControl.Style>
<Style TargetType="{x:Type TabControl}" BasedOn="{StaticResource {x:Type TabControl}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedItem.Visibility, ElementName=tabControl}"
Value="Collapsed">
<Setter Property="SelectedIndex" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.Style>
Run Code Online (Sandbox Code Playgroud)
我无法让触发器工作,并且VisibilityChanged我无法处理任何事件,所以我有点卡住并会感激一些帮助.