wpf Visibility.Collapsed 和 Visibility.Hidden 之间的区别

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

Difference between Visibility.Collapsed and Visibility.Hidden

wpfwpf-controlsvisibility

提问by Sauron

What are differences between Visibility.Collapsedand Visibility.Hiddenin WPF?

WPFVisibility.CollapsedVisibility.HiddenWPF之间有什么区别?

回答by Razzie

The difference is that Visibility.Hiddenhides the control, but reserves the space it occupies in the layout. So it renders whitespace instead of the control. Visibilty.Collapseddoes not render the control anddoes not reserve the whitespace. The space the control would take is 'collapsed', hence the name.

区别在于Visibility.Hidden隐藏控件,但保留它在布局中占用的空间。所以它呈现空白而不是控件。 Visibilty.Collapsed不呈现控件并且不保留空格。控件占用的空间是“折叠的”,因此得名。

The exact text from the MSDN:

来自 MSDN 的确切文本:

Collapsed: Do not display the element, and do not reserve space for it in layout.

Hidden: Do not display the element, but reserve space for the element in layout.

Visible: Display the element.

Collapsed:不显示元素,也不在布局中为其预留空间。

Hidden:不显示元素,但在布局中为元素保留空间。

可见:显示元素。

See: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx

请参阅:http: //msdn.microsoft.com/en-us/library/system.windows.visibility.aspx

回答by Kylo Ren

Visibility : Hidden Vs Collapsed

可见性:隐藏与折叠

Consider following code which only shows three Labelsand has second Labelvisibilityas Collapsed:

考虑下面的代码仅示出了three Labels与具有第二LabelvisibilityCollapsed

 <StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center">
    <StackPanel.Resources>
        <Style TargetType="Label">
            <Setter Property="Height" Value="30" />
            <Setter Property="Margin" Value="0"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="1" />
        </Style>
    </StackPanel.Resources>
    <Label Width="50" Content="First"/>
    <Label Width="50" Content="Second" Visibility="Collapsed"/>
    <Label Width="50" Content="Third"/>
</StackPanel>

Output Collapsed:

输出折叠:

Collapsed

折叠

Now change the second Labelvisibilityto Hiddden.

现在将第二个更改LabelvisibilityHiddden.

<Label Width="50" Content="Second" Visibility="Hidden"/>

Output Hidden:

输出隐藏:

Hidden

隐

As simple as that.

就如此容易。

回答by scsfdev

Even though a bit old thread, for those who still looking for the differences:

即使有点旧线程,对于那些仍在寻找差异的人:

Aside from layout (space) taken in Hidden and not taken in Collapsed, there is another difference.

除了在 Hidden 中采用的布局(空间)和在 Collapsed 中未采用的布局(空间)之外,还有另一个区别。

If we have custom controls inside this 'Collapsed' main control, the next time we set it to Visible, it will "load" all custom controls. It will not pre-load when window is started.

如果我们在这个 'Collapsed' 主控件中有自定义控件,下次我们将它设置为 Visible 时,它​​将“加载”所有自定义控件。窗口启动时不会预加载。

As for 'Hidden', it will load all custom controls + main control which we set as hidden when the "window" is started.

至于'隐藏',它会加载所有自定义控件+主控件,当“窗口”启动时我们设置为隐藏。