tags:

views:

367

answers:

3

Lets say I have two containers:

<StackPanel>
  <Label>First</Label>
</StackPanel>
<StackPanel>
  <Label>Second</Label>
</StackPanel>

And I'm bound to this object:

public class Model 
{
  public bool ShowFirst { get; set; }
  public bool ShowSecond { get; set; }
}

How would I set the binding to show and hide the respective panels?

+4  A: 

Bind the Visibility properties. You will need to use a BooleanToVisibilityConverter.

<!-- in the Resources section -->
<BooleanToVisibilityConverter x:Key="bvc" />

<!-- then -->
<Label Visibility="{Binding ShowFirst, Converter={StaticResource bvc}}">First</Label>

This assumes the DataContext is the model; otherwise you will also need to specify a source on the Binding.

By the way, this is probably just be because you have abbreviated the model code, but if you want to show and hide dynamically, your model will need to implement INotifyPropertyChanged.

itowlson
I think you probably need to set the DataContext to your model as well
Paul Betts
Good point, Paul -- I have updated the response to mention this. Thanks!
itowlson
A: 

You can use Event Triggers.

sipwiz
+2  A: 

I also found a good example here

bju1046