Search code examples
wpfbuttonvisibilitydockpanel

wpf remove button then add it in again at the same place


I have 3 buttons in a row inside a dockpanel (maybe this is not the right panel for me?). Each button is docked left.

|Button1|Button2|Button3|

I would like to dynamically remove any of the buttons and add them back at run time.

The buttons should always have the same order.

If I make Button1.Visibility = Visibility.Hidden then it dissapears but the space remains, which is not what i want, i want the other buttons to be shifted left like this:

|Button2|Button3|

If i remove and add a button like this:

DockButtons.Children.Remove(Button1); DockButtons.Children.Add(Button1);

It removes the Button1 from the left and adds it to the Right like this:

|Button2|Button3|Button1| 

Which again is not what I want. I want to keep the order of the buttons the same.

Any idea what I am doing wrong?

Many thanks in advance


Solution

  • You need to set the Visibility to collapsed, which will not render it, nor preserve the space it would consume.