2

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 Documentation there 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.

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:

<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;
            }
        }
    }
11
  • 2
    Don't use margins like that. Use another type of container such as a Grid or a DockPanel
    – Fede
    Jan 17, 2013 at 21:53
  • This comment is a bit unclear. If there is normally a margin of 10 on top/bottom and the control. height 50. is collapsed, expected margin between stacked controls should be 20, not 70.
    – sammarcow
    Jan 17, 2013 at 21:57
  • 3
    Still, don't use margins like that. Period.
    – Fede
    Jan 17, 2013 at 22:00
  • 1
    What kind of layout is Margin="0,386,31,432"?? Looks like you dragged and dropped UI elements in the designer. WPF is not for drag and droppers.
    – Fede
    Jan 17, 2013 at 22:06
  • 1
    @sammarcow There's a good chance that margins are respected even if the control is collapsed. Check this. Jan 17, 2013 at 22:11

1 Answer 1

2

When contol's visibility is in Collapsed state its margins do not participate in the layout (contrary to Hidden state)

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.

1
  • I typed the XAML, then went to the designer to quickly position the elements for the purposes of learning the controls and WPF architecture. I realize that is now a mistake. Collapse (and Margin on all the controls within a StackPanel) Properties are now respected.
    – sammarcow
    Jan 17, 2013 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.