System.Windows.Visibility崩溃与隐藏

时间:2013-01-17 21:51:09

标签: wpf user-interface layout wpf-controls

我有一个Visibility设置为“Collapsed”的控件和一个ToggleButton,它改变了Visibility.Collapsed和Visibility.Hidden之间的可见性。根据{{​​3}},当控件的Visibility设置为“Collaped”时,布局中的控件不应分配空间,但两个枚举之间没有视觉差异。此外,控件的可见性最初设置为Collapsed,因此控件的初始绘制不应为控件分配任何空间。

是否存在我缺少的概念,或者如何在可见时获取空间?我的最终目标是在基于用户选择的条件下显示控件,所述用户选择出现在所述显示变化控件的北部,所有控件之间具有一致的边距。

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

1 个答案:

答案 0 :(得分:2)

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

可以轻松验证:

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

我同意HighCore的看法,你XAML看起来只是从工具箱面板拖动控件。 VS的XAML设计师有这个令人不快的功能:它试图在边距的帮助下定位控件。