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.
Related reading
- How do I jump out of a foreach loop in C?
- How do I keep a label centered in WinForms?
- How do I keep my Async method thread safe?
- How do I list all loaded assemblies?
- How do I make a WinForms app go Full Screen
- How do I make calls to a REST API using C?
- How do I monitor clipboard content changes in C?
- How do I open an already opened file with a .net StreamReader?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.