Reputation: 95
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
Upvotes: 5
Views: 13454
Reputation: 2151
Natural option in the context of WPF is to use an MVVM pattern. So you have a control like
<Button Content="I'm a button"
Visibility={Binding BtnVis} />
And in your DataContext you would have a public property which you can set at your wish.
class DataContext
{
public Visibility BtnVis { get; set; }
}
Another usual option is to make use of a converter because, maybe, in your ViewModel you would like to have a bool property, not an UIElement.Visibility
one.
Upvotes: 2
Reputation: 370
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
Upvotes: 4
Reputation: 10744
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;
Upvotes: 13