Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to change control visibility in wpf

I find more questions on this theme, but I not find answer.

I need to changing visibility on control click.

In win form app, if I am right, I can use something like:

somecontrol.Visible = !somecontrol.Visible;

But when app is wpf I cannot use this way.

Is there way to do this on something "elegant" way then if-else?

Thanx

like image 303
maja Avatar asked Dec 03 '22 16:12

maja


2 Answers

In WPF UIElement.Visibility has 3 states; Visible/Hidden/Collapsed.

If hidden, the control will still affect the layout of surrounding controls, elements that have a Visibility value of Collapsed do not occupy any layout space.

You can switch between visible and hidden or collapsed.

somecontrol.Visibility = somecontrol.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
like image 127
Glen Thomas Avatar answered Dec 05 '22 04:12

Glen Thomas


In WPF the property you're trying to change is called Visibility u can use it as described below..

uiElement.Visibility = Visibility.Hidden;
                    // Visibility.Collapsed;
                    // Visibility.Visible;

The states interact like @Glen already mentioned. What do you mean by..

Is there way to do this on something "elegant" way then if-else?

If you don't want to write the whole construct just use the shorter form..

uiElement.Visibility = uiElement.Visibility == Visibility.Visible // condition
    ? Visibility.Collapsed // if-case
    : Visibility.Visible;  // else-case
like image 45
Jan Unld Avatar answered Dec 05 '22 06:12

Jan Unld