wpf C# 窗口可见性,折叠和隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19652499/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:58:19  来源:igfitidea点击:

C# Window Visibility, collapsed and hidden

c#wpfvisibilityhidden

提问by Ben Cheng

I have a quick question regarding visibility of windows in an application. According to... http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx(its short)

我有一个关于应用程序中窗口可见性的快速问题。根据... http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx(简称)

When a window is collapsed no space is reserved for the window in layout. When a window is hidden space is reserved for the window in layout.

当窗口折叠时,不会为布局中的窗口保留空间。当一个窗口被隐藏时,会为布局中的窗口保留空间。

I'm confused here, what is the layout referring to? Is it referring to window space?

我在这里很困惑,布局指的是什么?它是指窗口空间吗?

回答by Tico

Here's an illustration:

这是一个插图:

 <Grid>
        <TabControl>
            <TabItem Header="Visible"></TabItem>
            <TabItem Visibility="Hidden" Header="Hidden">Hidden</TabItem>
            <TabItem Visibility="Hidden" Header="Hidden">Hidden</TabItem>
            <TabItem Visibility="Hidden" Header="Hidden">Hidden</TabItem>
            <TabItem Header="Visible"></TabItem>
            <TabItem Header="Visible"></TabItem>
            <TabItem Header="Visible"></TabItem>
        </TabControl>
    </Grid>

Will render this:
enter image description here

将呈现这个:
在此处输入图片说明

And this XAML:

这个 XAML:

<Grid>
    <TabControl>
        <TabItem Header="Visible"></TabItem>
        <TabItem Visibility="Collapsed" Header="Collapsed">Collapsed</TabItem>
        <TabItem Visibility="Collapsed" Header="Collapsed">Collapsed</TabItem>
        <TabItem Visibility="Collapsed" Header="Collapsed">Collapsed</TabItem>
        <TabItem Header="Visible"></TabItem>
        <TabItem Header="Visible"></TabItem>
        <TabItem Header="Visible"></TabItem>
    </TabControl>
</Grid>

Will render this:

将呈现这个:

enter image description here

在此处输入图片说明

So, Collapsedwill not save the space, whereas Hiddenwill.

所以,Collapsed不会节省空间,而Hidden会。

回答by pazcal

No, its referring to the whole window you are looking at.

不,它指的是您正在查看的整个窗口。

Lets say, you have at the top of the screen a Red Block (20px height) and below the Red Block you have a title.

假设您在屏幕顶部有一个红块(20 像素高),在红块下方有一个标题。

Hidden:The Red Block is NOT visible, but the space it normally reserves, is still reserved, meaning the Title is is 20px away from the top of the screen

隐藏:红色块不可见,但它通常保留的空间仍然保留,这意味着标题距离屏幕顶部 20px

Collapsed:The Red Block is NOT visible together with the reserved space (the 20px height), meaning the Title is located at the top of the screen

折叠:红色块与保留空间(20px 高度)不可见,这意味着标题位于屏幕顶部

回答by EricSchaefer

The visibility does not only refer to windows, but all controls. If you use a layout that automatically places its child controls that it makes a difference if you use 'hidden' or 'collapsed'. 'hidden' means that the layout control still "reserves space" for it when arranging its children, while 'collapsed' means that the layout is not reserving any space for it.

可见性不仅指窗口,还指所有控件。如果您使用自动放置其子控件的布局,则使用“隐藏”或“折叠”会有所不同。'hidden' 表示布局控件在安排其子项时仍为其“保留空间”,而 'collapsed' 表示布局未为其保留任何空间。

回答by CHash_Mike

Layout is basically the overall placing of your controls within the form so if its collapsed that means it would be abscent in the UI and its place would be utilized by other controls however when its hidden it would be just invisible to user however its place can not be occupied by any other control its just not visible to user.

布局基本上是您的控件在表单中的整体放置,因此如果它折叠起来,这意味着它在 UI 中将不存在,并且它的位置将被其他控件使用,但是当它隐藏时,它对用户不可见,但它的位置不能被任何其他控件占用,它只是对用户不可见。