简体   繁体   中英

How to hide and show content in wpf?

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?

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.

您可以使用事件触发器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM