Generic boolean value converter for WPF

When implementing user interfaces with WPF and using databinding you often need to show something differently based on boolean value in your view model. Here is a clever converter to help you.

Sample usage

<GenericBooleanConverter x:Key="booleanToVisibility" TrueValue="Visible" FalseValue="Collapsed" />
<GenericBooleanConverter x:Key="boolToOrientation" TrueValue="Vertical" FalseValue="Horizontal" />

<StackPanel
Orientation="{Binding IsMyViewModelVertical, Converter={StaticResource booleanToOrientation}}"
Visibility="{Binding IsMyViewModelVisible, Converter={StaticResource booleanToVisibility}}">
<Label>I am a label that is sometimes visible</Label>
<Label>I am a label that is sometimes to the left and sometimes below the previous label</Label>
</StackPanel>

Advertisement

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.