BooleanToVisibilityConverter
Invert BooleanToVisibilityConverter
XAML
WPF
Data Binding

How do I invert BooleanToVisibilityConverter?

Codemia Knowledge Hub ·

Understanding BooleanToVisibilityConverter and Its Inversion

In the context of building user interfaces with WPF (Windows Presentation Foundation), data binding and converters play a crucial role in managing how data is displayed and interacted with. One commonly-used converter is the `BooleanToVisibilityConverter`, which is used to map a boolean property to a `Visibility` state (`Visible`, `Collapsed`, or `Hidden`). However, there might be scenarios where you need to invert the behavior of this converter. This article will explore how to implement an inverted `BooleanToVisibilityConverter` to suit such requirements.

BooleanToVisibilityConverter: A Quick Overview

The `BooleanToVisibilityConverter` is part of WPF’s data binding infrastructure that converts boolean values into WPF `Visibility` enumeration values. The standard `BooleanToVisibilityConverter` maps `true` to `Visibility.Visible` and `false` to `Visibility.Collapsed` by default.

How It Works

  • True: Maps to `Visibility.Visible`.
  • False: Maps to `Visibility.Collapsed`.

This default behavior can be altered by setting the `TrueVisibility` and `FalseVisibility` properties if using a custom converter implementation. However, in many cases, developers may wish to invert this behavior such that `true` corresponds to a collapsed or hidden state and `false` corresponds to a visible state.

Inverting the BooleanToVisibilityConverter

To invert the default behavior, you can create a custom converter that inherits from `IValueConverter` and implements the inversion logic. Here is a step-by-step guide with code implementation.

Custom InvertedBooleanToVisibilityConverter

First, you will need to create a new class that implements `IValueConverter`.

  • Convert Method: The `Convert` method checks if the input value is a boolean. If `true`, it returns `Visibility.Collapsed`, and if `false`, it returns `Visibility.Visible`.
  • ConvertBack Method: This is crucial for two-way binding scenarios where the value needs to be converted back. The method ensures symmetry by converting `Visibility.Collapsed` back to `true`.
  • Declaring the Converter: The converter is declared in the window’s resources with a specific key `InvertedBoolToVis`.
  • Applying the Converter: The `Visibility` property of the button is bound to a boolean property in the data context. The `InvertedBooleanToVisibilityConverter` is applied to invert the visibility logic.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms