سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Using visibility collapsed instead of visibility hidden will not keep the space for it or render it. So changing your implementation to collapsed should fix that.

Alternatively (maybe not preferred depending on how it looks to the user, and that it's more code) When you want to remove a button, You could remove all buttons and then add all the buttons you want displayed in the order you want them for ex)

If you want to remove button 1. Remove all buttons, then add button 2, then add button 3

Though as a I said first visibility collapsed would be ideal.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top