Changing Visibility in a StackPanel

abelenky picture abelenky · Mar 7, 2011 · Viewed 25.6k times · Source

I have a WPF StackPanel that looks like this: (some attributes removed that don't matter)

<StackPanel HorizontalAlignment="Center" Name="PICStack">
        <Label Name="PICName"  MouseDoubleClick="PICName_MouseDoubleClick" />
        <TextBox Name="PICData" Width="120" Visibility="Hidden" />
        <Label Name="PICWeight" />
        <Label Name="PICARM"    />
</StackPanel>

Note that the TextBox starts as "Hidden".

When I double-click on the top label, I swap the visibility:

private void PICName_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    this.PICData.Visibility = Visibility.Visible;
    this.PICName.Visibility = Visibility.Hidden;
}

The intent is to hide the label, and make the TextBox appear in its place.

However, because it is a StackPanel, the TextBox takes up vertical space, even when it is not visible. Then, when the TextBox is revealed, it has blank space above it, where the Label was previously visible.

Is there a good way to make the two items essentially be directly on top of each other? so that double-clicking the Label appears to suddenly change into a TextBox?

Answer

Dean Kuga picture Dean Kuga · Mar 7, 2011

Use Visibilty.Collapsed instead. It doesn't reserve the whitespace like Visibilty.Hidden does.