wpf System.Windows.Visibility 折叠与隐藏

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

System.Windows.Visibility Collapsed vs Hidden

wpfuser-interfacelayoutwpf-controls

提问by sammarcow

I have a control with Visibility set to "Collapsed" and a ToggleButton that changes the visibility between Visibility.Collapsed and Visibility.Hidden. According to the MSDN Documentationthere should be no space allotted for the control within the layout when the control's Visibility is set to "Collaped," but there are no visual differences between the two enumerations. Additionally, the Visibility of the control is initially set to Collapsed so the initial drawing of the controls should not allot any space for the control.

我有一个 Visibility 设置为“Collapsed”的控件和一个 ToggleButton,用于更改 Visibility.Collapsed 和 Visibility.Hidden 之间的可见性。根据 MSDN 文档,当控件的可见性设置为“折叠”时,布局内不应为控件分配空间,但两个枚举之间没有视觉差异。此外,控件的可见性最初设置为折叠,因此控件的初始绘制不应为控件分配任何空间。

Is there a concept I am missing, or how do I get an element to take space only when visible? My end-goal is to have controls appear on conditions based on user-selections that appear north of said display-varied controls, with consistent margins between all controls .

是否有我遗漏的概念,或者如何让元素仅在可见时占用空间?我的最终目标是让控件出现在基于用户选择的条件下,这些用户选择出现在所述显示变化的控件以北,所有控件之间的边距保持一致。

XAML Snippet:

XAML 代码段:

<StackPanel>
    <TextBox Name="hideTest" DataContext="{StaticResource persistentMemoryBridge}"   Text="HIDETEST" Margin="0,327,31,491" Foreground="Black" Background="Orange" Visibility="Collapsed" />
    <TextBox DataContext="{StaticResource persistentMemoryBridge}"   Text="{Binding Path=PropertyTest}" Margin="0,386,31,432" Foreground="Black" Background="Yellow"/>
    <ToggleButton Name="tbVisibility" Content="Toggle" Click="ToggleButton_Click" Margin="0,445,65,391"></ToggleButton>
</StackPanel>

CodeBehind:

代码隐藏:

private void ToggleButton_Click(object sender, RoutedEventArgs e) {

        switch (hideTest.Visibility) {
            case System.Windows.Visibility.Collapsed: {
                hideTest.Visibility = Visibility.Hidden;
                tbVisibility.Content = "Hidden";
                break;    
            }
        case System.Windows.Visibility.Hidden: {
            hideTest.Visibility = Visibility.Visible;
            tbVisibility.Content = "Visible";    
            break;
            }
        case System.Windows.Visibility.Visible: {
            hideTest.Visibility = Visibility.Collapsed;
            tbVisibility.Content = "Collapsed";
            break;
            }
        }
    }

采纳答案by Pavel Voronin

When contol's visibility is in Collapsedstate its margins do not participate in the layout (contrary to Hiddenstate)

当控制的可见性处于Collapsed状态时,它的边距不参与布局(与Hidden状态相反)

Can be verified easily:

可以很容易地验证:

<Window x:Class="MarginsRespectForCollapsedTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Margin="50,50" Visibility="Collapsed">I'm Collapsed</Button>
        <Button>I'm Visible!</Button>
    </StackPanel>
</Window>

I agree with HighCore that you XAML really looks like you just dragged controls from the toolbox panel. VS's XAML designer has this unpleasant feature: it tries to position controls with the help of margins.

我同意 HighCore 的观点,您的 XAML 看起来就像您只是从工具箱面板中拖动控件一样。VS 的 XAML 设计器有一个令人不快的特性:它试图借助边距来定位控件。