Reputation: 2956
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;
}
}
}
Upvotes: 2
Views: 6171
Reputation: 13983
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.
Upvotes: 2