18

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?

2 Answers 2

36

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

11

You should try using Visibility.Collapsed instead of Visibility.Hidden.

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.