wpf TabControl:所有 TabItem 都已折叠,但第一个 TabItem 的内容仍然可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21164098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TabControl: all TabItems collapsed, but content of 1st TabItem still visible
提问by ThorstenHa
I've got a rather strange behavior on a TabControl, whose TabItems are all collapsed: The content of the first TabItem is still visible (but the header is not).
我在 TabControl 上有一个相当奇怪的行为,它的 TabItems 都折叠了:第一个 TabItem 的内容仍然可见(但标题不是)。
The TabControl and its TabItems are setup like this:
TabControl 及其 TabItem 设置如下:
<TabControl>
<TabItem Header="Data 1"
Visibility="{Binding Path=DataTable1.HasRows,
Converter={StaticResource BoolToVisibility}}">
<UI:ShowData DataContext="{Binding Path=DataTable1}"/>
</TabItem>
<TabItem Header="Data 2"
Visibility="{Binding Path=DataTable2.HasRows,
Converter={StaticResource BoolToVisibility}}">
<UI:ShowData DataContext="{Binding Path=DataTable2}"/>
</TabItem>
</TabControl>
If none of the data tables contains any rows, none of the TabItems should be shown. (I known that I could hide the whole TabControl in that case, but that's not the point here.)
如果没有数据表包含任何行,则不应显示任何 TabItem。(我知道在这种情况下我可以隐藏整个 TabControl,但这不是重点。)
Actually the content of the tab item "Header 1" will be displayed despite the TabItem being collapsed! The TabItem's header itself is collapsed, the TabItems border which contains its content and the content itself are displayed.
实际上,尽管 TabItem 已折叠,但仍会显示选项卡项“Header 1”的内容!TabItem 的标题本身被折叠,显示包含其内容的 TabItems 边框和内容本身。
Edit/Add: This can easily be reproduced using this code (note using Collapsed or Hidden does not make any difference:
编辑/添加:这可以使用此代码轻松重现(注意使用 Collapsed 或 Hidden 没有任何区别:
<TabControl>
<TabItem Header="Test 1" Visibility="Hidden">
<Label>Test1</Label>
</TabItem>
<TabItem Header="Test 2" Visibility="Hidden">
<Label>Test2</Label>
</TabItem>
</TabControl>
So what's wrong here? Any help/hints are appreciated!
那么这里有什么问题呢?任何帮助/提示表示赞赏!
回答by Sheridan
Ok, so you've found a real problem here... I looked around online and found several posts that relate to this. Some say that this is a bug, while others say that it is the designed behaviour. don't know which, although it certainly seems to be more of a bug than a feature.
好的,所以你在这里发现了一个真正的问题......我在网上环顾四周,发现了几篇与此相关的帖子。有人说这是一个错误,而另一些人说这是设计的行为。不知道是哪个,尽管它似乎更像是一个错误而不是一个功能。
Either way, you want to know how to deal with the problem. .. there are several solutions. One is just to set the TabItem.Contentto nullwhenever you want to hide the tab and another is another involves adding an empty TabItemand selecting that item before hiding (so that it's empty content is shown).
无论哪种方式,您都想知道如何处理问题。.. 有几种解决方案。一种是只设置TabItem.Content到null时候你想隐藏的标签,另一个是另一个涉及添加一个空的TabItem和隐藏之前选择该项目(以便它的内容为空所示)。
You can attach a handler to the TabItem.IsVisibleChangedEventto be notified when the Visibilityproperty has been changed:
您可以将处理程序附加到TabItem.IsVisibleChanged事件以在Visibility属性更改时收到通知:
public void TabItemIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
// Hide TabItem.Content here
}
Here are some links to the relevant posts:
以下是相关帖子的一些链接:
Bug in TabControl/TabItem`s content visibility?
WPF TabControl - Select different tab when TabItem Visibility changes
Is there a workaround for this tabcontrol/tabitem bug
TabControl/TabItem 的内容可见性中的错误?
WPF TabControl - TabItem 可见性更改时选择不同的选项卡
是否有解决此 tabcontrol/tabitem 错误的方法
回答by taters613
Another solution that I prefer over the ones suggested: Bind the visibility of the TabItem and its contentto the same property (using the BooleanToVisibilityConverter). Here's a simple example:
另一个我更喜欢的解决方案是:将 TabItem 的可见性及其内容绑定到相同的属性(使用 BooleanToVisibilityConverter)。这是一个简单的例子:
<UserControl.Resources >
<BooleanToVisibilityConverter x:Key="boolToVis"/>
</UserControl.Resources>
<Grid>
<TabControl>
<TabItem Header="TabItem 1" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}">
<Label Content="Content 1" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}"/>
</TabItem>
<TabItem Header="TabItem 2" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}">
<Label Content="Content 2" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}"/>
</TabItem>
</TabControl>
</Grid>
回答by Husam Saadi
Could be a WPF bug, anyway bypass by binding the content visibility to the tab item visibility.
可能是 WPF 错误,无论如何通过将内容可见性绑定到选项卡项可见性来绕过。
<TabControl>
<TabItem x:Name="_test1Tab" Header="Test 1" Visibility="Hidden">
<Label Visibility="{Binding ElementName=_test1Tab, Path=Visibility}">Test1</Label>
</TabItem>
<TabItem x:Name="_test2Tab" Header="Test 2" Visibility="Hidden">
<Label Visibility="{Binding ElementName=_test1Tab, Path=Visibility}">Test2</Label>
</TabItem>
</TabControl>
回答by Andrea Antonangeli
My solution to this was to put the TabItem I wanted to hide in another position. The problem happens only if you want to collapse only the first TabItem.
我对此的解决方案是将我想隐藏的 TabItem 放在另一个位置。只有当您只想折叠第一个 TabItem 时才会出现问题。

