TabControl:所有 TabItem 已折叠,但第一个 TabItem 的内容仍然可见

编程入门 行业动态 更新时间:2024-10-10 06:15:54
本文介绍了TabControl:所有 TabItem 已折叠,但第一个 TabItem 的内容仍然可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在 TabControl 上有一个相当奇怪的行为,它的 TabItems 都折叠了:第一个 TabItem 的内容仍然可见(但标题不可见).

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 及其 TabItem 设置如下:

The TabControl and its TabItems are setup like this:

<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>

如果没有数据表包含任何行,则不应显示任何 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 被折叠,标签项Header 1"的内容仍会显示!TabItem 的标题本身被折叠,显示包含其内容的 TabItems 边框和内容本身.

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.

编辑/添加:这可以使用此代码轻松重现(注意使用 Collapsed 或 Hidden 没有任何区别:

Edit/Add: This can easily be reproduced using this code (note using Collapsed or Hidden does not make any difference:

<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!

推荐答案

好的,你在这里发现了一个真正的问题......我在网上环顾四周,发现了几个与此相关的帖子.有人说这是一个错误,而另一些人说这是设计的行为.不知道是哪个,虽然它看起来更像是一个错误而不是一个功能.

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.

无论哪种方式,您都想知道如何处理问题... 有几种解决方案.一种是在您想隐藏选项卡时将 TabItem.Content 设置为 null,另一种是添加一个空的 TabItem 并选择隐藏之前的项目(以便显示空内容).

Either way, you want to know how to deal with the problem. .. there are several solutions. One is just to set the TabItem.Content to null whenever you want to hide the tab and another is another involves adding an empty TabItem and selecting that item before hiding (so that it's empty content is shown).

您可以将处理程序附加到 TabItem.IsVisibleChanged 事件在 Visibility 属性更改时收到通知:

You can attach a handler to the TabItem.IsVisibleChanged Event to be notified when the Visibility property has been changed:

public void TabItemIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    // Hide TabItem.Content here
}

以下是相关帖子的一些链接:

Here are some links to the relevant posts:

TabControl/TabItem 的内容可见性有问题吗?
WPF TabControl - 当 TabItem 可见性改变时选择不同的选项卡
是否有针对此 tabcontrol/tabitem 错误的解决方法

这篇关于TabControl:所有 TabItem 已折叠,但第一个 TabItem 的内容仍然可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:37:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   内容   TabControl   TabItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!