I'm using a BooleanToVisibilityConverter
in WPF to bind the Visibility
property of a control to a Boolean
. This works fine, but I'd like one of the controls to hide if the boolean is true
, and show if it's false
.

- 59,778
- 26
- 187
- 249
-
note: as of beta 4 - silverlight does not include BooleanToVisibility - so you'll need to implement it yourself anyway – Simon_Weaver Dec 21 '09 at 03:52
-
Added a user voice suggestion to get invert supported http://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/11083446-booleantovisibilityconverter-should-allow-inverted – Thraka Dec 14 '15 at 16:47
-
I can''t believe they didn't implemented some converter parameters to do such things. – Kamil Jul 05 '19 at 23:27
-
1Simplest solution I found was to create a property in the Model that returns negative value of the original. public bool ShowMyControl { get { return !theValue; } } In WPF Visibility="{Binding MyModel.ShowMyControl, Converter={StaticResource BooleanToVisibilityConverter}}" – Zamir Jan 18 '22 at 17:38
17 Answers
Instead of inverting, you can achieve the same goal by using a generic IValueConverter
implementation that can convert a Boolean value to configurable target values for true and false. Below is one such implementation:
public class BooleanConverter<T> : IValueConverter
{
public BooleanConverter(T trueValue, T falseValue)
{
True = trueValue;
False = falseValue;
}
public T True { get; set; }
public T False { get; set; }
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is bool && ((bool) value) ? True : False;
}
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is T && EqualityComparer<T>.Default.Equals((T) value, True);
}
}
Next, subclass it where T
is Visibility
:
public sealed class BooleanToVisibilityConverter : BooleanConverter<Visibility>
{
public BooleanToVisibilityConverter() :
base(Visibility.Visible, Visibility.Collapsed) {}
}
Finally, this is how you could use BooleanToVisibilityConverter
above in XAML and configure it to, for example, use Collapsed
for true and Visible
for false:
<Application.Resources>
<app:BooleanToVisibilityConverter
x:Key="BooleanToVisibilityConverter"
True="Collapsed"
False="Visible" />
</Application.Resources>
This inversion is useful when you want to bind to a Boolean property named IsHidden
as opposed IsVisible
.

- 36,108
- 16
- 64
- 74
-
I might be missing something, but don't you just need a negated property ? http://stackoverflow.com/questions/534575/how-do-i-invert-booleantovisibilityconverter/11800656#11800656 – OscarRyz Aug 03 '12 at 17:42
-
11@OscarRyz: With more complex UIs, that starts to add a lot of really annoying clutter to the view models, not to mention another property you theoretically have to unit test in order to maintain code coverage. View models shouldn't have to get *that* close to the implementation details of the view, otherwise you might as well just have `Visibility` properties in your view model. – Aaronaught Nov 04 '12 at 21:27
-
Write your own is the best solution for now. Here is an example of a Converter that can do both way Normal and Inverted. If you have any problems with this just ask.
[ValueConversion(typeof(bool), typeof(Visibility))]
public class InvertableBooleanToVisibilityConverter : IValueConverter
{
enum Parameters
{
Normal, Inverted
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var boolValue = (bool)value;
var direction = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);
if(direction == Parameters.Inverted)
return !boolValue? Visibility.Visible : Visibility.Collapsed;
return boolValue? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}
<UserControl.Resources>
<Converters:InvertableBooleanToVisibilityConverter x:Key="_Converter"/>
</UserControl.Resources>
<Button Visibility="{Binding IsRunning, Converter={StaticResource _Converter}, ConverterParameter=Inverted}">Start</Button>

- 34,632
- 9
- 60
- 82

- 1,188
- 12
- 12
-
2Just wonding one thing. The xaml code "Binding IsRunning", where is the sourcode or the value for the object "IsRunning"? – HelloWorld1 Aug 10 '11 at 08:27
-
IsRunning is a property on my viewmodel. The context of this code is long but the short of it is that I needed to have somethings hidden when I was running some calculation and other things not hidden. I created this converter to make it so I would not have to have multiple properties on my viewmodel. – Michael Hohlios Aug 25 '11 at 17:37
-
2You can make it a drop-in replacement for the normal `BooleanToVisibilityConverter` by checking the parameter for null: `Parameter direction = Parameter.Normal; if (parameter != null) direction = (Parameter)Enum.Parse(typeof(Parameter), (string)parameter);` – JCH2k Dec 01 '14 at 17:20
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var flag = false;
if (value is bool)
{
flag = (bool)value;
}
else if (value is bool?)
{
var nullable = (bool?)value;
flag = nullable.GetValueOrDefault();
}
if (parameter != null)
{
if (bool.Parse((string)parameter))
{
flag = !flag;
}
}
if (flag)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var back = ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
if (parameter != null)
{
if ((bool)parameter)
{
back = !back;
}
}
return back;
}
}
and then pass a true or false as the ConverterParameter
<Grid.Visibility>
<Binding Path="IsYesNoButtonSetVisible" Converter="{StaticResource booleanToVisibilityConverter}" ConverterParameter="true"/>
</Grid.Visibility>

- 154
- 1
- 2
- 11

- 33,714
- 21
- 133
- 202
-
4At the `else if (value is bool?)` part, ReSharper tells me "Expression is always false". Also, the `if (flag)` part can be rewritten more concisely as `return flag ? Visibility.Visible : Visibility.Collapsed;`. – Danilo Bargen Jan 06 '12 at 21:08
-
1I might be missing something, but don't you just need a negated property ? http://stackoverflow.com/questions/534575/how-do-i-invert-booleantovisibilityconverter/11800656#11800656 – OscarRyz Aug 03 '12 at 17:43
-
1`var nullable = (bool?)value; flag = nullable.GetValueOrDefault();` can be made much shorter and simple: `flag = (bool?)value ?? false;` – ANeves Feb 25 '14 at 15:04
One more way to Bind ViewModel Boolean Value (IsButtonVisible) with xaml Control Visibility Property. No coding, No converting, just styling.
<Style TargetType={x:Type Button} x:Key="HideShow">
<Style.Triggers>
<DataTrigger Binding="{Binding IsButtonVisible}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Button Style="{StaticResource HideShow}">Hello</Button>

- 5,781
- 3
- 39
- 61

- 930
- 13
- 15
Implement your own implementation of IValueConverter. A sample implementation is at
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
In your Convert method, have it return the values you'd like instead of the defaults.

- 8,548
- 6
- 47
- 55

- 5,268
- 1
- 28
- 56
There's also the WPF Converters project on Codeplex. In their documentation they say you can use their MapConverter to convert from Visibility enumeration to bool
<Label>
<Label.Visible>
<Binding Path="IsVisible">
<Binding.Converter>
<con:MapConverter>
<con:Mapping From="True" To="{x:Static Visibility.Visible}"/>
<con:Mapping From="False" To="{x:Static Visibility.Hidden}"/>
</con:MapConverter>
</Binding.Converter>
</Binding>
</Label.Visible>
</Label>

- 12,737
- 10
- 51
- 76

- 70,676
- 20
- 104
- 133
-
1WPF Converters now includes a BooleanToVisibilityConverter that can be reversed. – vinod Mar 27 '13 at 07:38
Or the real lazy mans way, just make use of what is there already and flip it:
public class InverseBooleanToVisibilityConverter : IValueConverter
{
private BooleanToVisibilityConverter _converter = new BooleanToVisibilityConverter();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var result = _converter.Convert(value, targetType, parameter, culture) as Visibility?;
return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var result = _converter.ConvertBack(value, targetType, parameter, culture) as bool?;
return result == true ? false : true;
}
}

- 1,075
- 1
- 11
- 15
If you don't like writing custom converter, you could use data triggers to solve this:
<Style.Triggers>
<DataTrigger Binding="{Binding YourBinaryOption}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding YourBinaryOption}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>

- 741
- 7
- 13
Here's one I wrote and use a lot. It uses a boolean converter parameter that indicates whether or not to invert the value and then uses XOR to perform the negation:
[ValueConversion(typeof(bool), typeof(System.Windows.Visibility))]
public class BooleanVisibilityConverter : IValueConverter
{
System.Windows.Visibility _visibilityWhenFalse = System.Windows.Visibility.Collapsed;
/// <summary>
/// Gets or sets the <see cref="System.Windows.Visibility"/> value to use when the value is false. Defaults to collapsed.
/// </summary>
public System.Windows.Visibility VisibilityWhenFalse
{
get { return _visibilityWhenFalse; }
set { _visibilityWhenFalse = value; }
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool negateValue;
Boolean.TryParse(parameter as string, out negateValue);
bool val = negateValue ^ System.Convert.ToBoolean(value); //Negate the value when negateValue is true using XOR
return val ? System.Windows.Visibility.Visible : _visibilityWhenFalse;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool negateValue;
Boolean.TryParse(parameter as string, out negateValue);
if ((System.Windows.Visibility)value == System.Windows.Visibility.Visible)
return true ^ negateValue;
else
return false ^ negateValue;
}
}
Here's an XOR truth table for reference:
XOR
x y XOR
---------
0 0 0
0 1 1
1 0 1
1 1 0

- 12,621
- 7
- 81
- 125
I just did a post on this. I used a similar idea as Michael Hohlios did. Only, I used Properties instead of using the "object parameter".
Binding Visibility to a bool value in WPF
Using Properties makes it more readable, in my opinion.
<local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Collapse="True" Reverse="True" />

- 6,510
- 2
- 44
- 50
-
Just a follow-up on my own comment. If you use Properties, you have to create a separate object if you want to create to converters, one that is Reverse and one not. If you use parameters, you can use one object for multiple items, but it can be confusing if you don't pay attention. So there are pros and cons to both. – Rhyous Jul 25 '11 at 19:24
-
I found this very helpful in realizing Boolean to Colors converters. Thank you – Federinik Apr 07 '15 at 17:44
Rather than writing your own code / reinventing, consider using CalcBinding:
// Automatic two way convertion of bool expression to Visibility and
// back if target property has such type: description
<Button Visibility="{c:Binding !IsChecked}" />
<Button Visibility="{c:Binding IsChecked, FalseToVisibility=Hidden}" />
CalcBinding is also quite useful for numerous other scenarios.

- 11,743
- 10
- 52
- 81
I was looking for a more general answer, but could not find it. I wrote a converter that might help others.
It is based on the fact that we need to distinguish six different cases:
- True 2 Visible, False 2 Hidden
- True 2 Visible, False 2 Collapsed
- True 2 Hidden, False 2 Visible
- True 2 Collapsed, False 2 Visible
- True 2 Hidden, False 2 Collapsed
- True 2 Collapsed, False 2 Hidden
Here is my implementation for the first 4 cases:
[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : IValueConverter
{
enum Types
{
/// <summary>
/// True to Visible, False to Collapsed
/// </summary>
t2v_f2c,
/// <summary>
/// True to Visible, False to Hidden
/// </summary>
t2v_f2h,
/// <summary>
/// True to Collapsed, False to Visible
/// </summary>
t2c_f2v,
/// <summary>
/// True to Hidden, False to Visible
/// </summary>
t2h_f2v,
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var b = (bool)value;
string p = (string)parameter;
var type = (Types)Enum.Parse(typeof(Types), (string)parameter);
switch (type)
{
case Types.t2v_f2c:
return b ? Visibility.Visible : Visibility.Collapsed;
case Types.t2v_f2h:
return b ? Visibility.Visible : Visibility.Hidden;
case Types.t2c_f2v:
return b ? Visibility.Collapsed : Visibility.Visible;
case Types.t2h_f2v:
return b ? Visibility.Hidden : Visibility.Visible;
}
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
var v = (Visibility)value;
string p = (string)parameter;
var type = (Types)Enum.Parse(typeof(Types), (string)parameter);
switch (type)
{
case Types.t2v_f2c:
if (v == Visibility.Visible)
return true;
else if (v == Visibility.Collapsed)
return false;
break;
case Types.t2v_f2h:
if (v == Visibility.Visible)
return true;
else if (v == Visibility.Hidden)
return false;
break;
case Types.t2c_f2v:
if (v == Visibility.Visible)
return false;
else if (v == Visibility.Collapsed)
return true;
break;
case Types.t2h_f2v:
if (v == Visibility.Visible)
return false;
else if (v == Visibility.Hidden)
return true;
break;
}
throw new InvalidOperationException();
}
}
example:
Visibility="{Binding HasItems, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter='t2v_f2c'}"
I think the parameters are easy to remember.
Hope it helps somebody.

- 3,513
- 1
- 15
- 28
You can use QuickConverter.
With QuickConverter you can write the converter logic inline with your BindingExpression
Here is an inverted BooleanToVisibility converter:
Visibility="{qc:Binding '!$P ? Visibility.Visible : Visibility.Collapsed', P={Binding Example}}"
You can add QuickConverter via NuGet. Have a look at the documentation for setup. Link: https://quickconverter.codeplex.com/

- 2,344
- 1
- 25
- 27
Write your own convert.
public class ReverseBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// your converter code here
}
}

- 28,542
- 5
- 55
- 68
A simple one way version which can be used like this:
Visibility="{Binding IsHidden, Converter={x:Static Ui:Converters.BooleanToVisibility}, ConverterParameter=true}
can be implemented like this:
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var invert = false;
if (parameter != null)
{
invert = Boolean.Parse(parameter.ToString());
}
var booleanValue = (bool) value;
return ((booleanValue && !invert) || (!booleanValue && invert))
? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

- 4,814
- 1
- 26
- 24
Convert everything to everything (bool,string,enum,etc):
public class EverythingConverterValue
{
public object ConditionValue { get; set; }
public object ResultValue { get; set; }
}
public class EverythingConverterList : List<EverythingConverterValue>
{
}
public class EverythingConverter : IValueConverter
{
public EverythingConverterList Conditions { get; set; } = new EverythingConverterList();
public object NullResultValue { get; set; }
public object NullBackValue { get; set; }
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return Conditions.Where(x => x.ConditionValue.Equals(value)).Select(x => x.ResultValue).FirstOrDefault() ?? NullResultValue;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return Conditions.Where(x => x.ResultValue.Equals(value)).Select(x => x.ConditionValue).FirstOrDefault() ?? NullBackValue;
}
}
XAML Examples:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:MvvmGo.Converters;assembly=MvvmGo.WindowsWPF"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<conv:EverythingConverter x:Key="BooleanToVisibilityConverter">
<conv:EverythingConverter.Conditions>
<conv:EverythingConverterValue ResultValue="{x:Static Visibility.Visible}">
<conv:EverythingConverterValue.ConditionValue>
<sys:Boolean>True</sys:Boolean>
</conv:EverythingConverterValue.ConditionValue>
</conv:EverythingConverterValue>
<conv:EverythingConverterValue ResultValue="{x:Static Visibility.Collapsed}">
<conv:EverythingConverterValue.ConditionValue>
<sys:Boolean>False</sys:Boolean>
</conv:EverythingConverterValue.ConditionValue>
</conv:EverythingConverterValue>
</conv:EverythingConverter.Conditions>
</conv:EverythingConverter>
<conv:EverythingConverter x:Key="InvertBooleanToVisibilityConverter">
<conv:EverythingConverter.Conditions>
<conv:EverythingConverterValue ResultValue="{x:Static Visibility.Visible}">
<conv:EverythingConverterValue.ConditionValue>
<sys:Boolean>False</sys:Boolean>
</conv:EverythingConverterValue.ConditionValue>
</conv:EverythingConverterValue>
<conv:EverythingConverterValue ResultValue="{x:Static Visibility.Collapsed}">
<conv:EverythingConverterValue.ConditionValue>
<sys:Boolean>True</sys:Boolean>
</conv:EverythingConverterValue.ConditionValue>
</conv:EverythingConverterValue>
</conv:EverythingConverter.Conditions>
</conv:EverythingConverter>
<conv:EverythingConverter x:Key="MarriedConverter" NullResultValue="Single">
<conv:EverythingConverter.Conditions>
<conv:EverythingConverterValue ResultValue="Married">
<conv:EverythingConverterValue.ConditionValue>
<sys:Boolean>True</sys:Boolean>
</conv:EverythingConverterValue.ConditionValue>
</conv:EverythingConverterValue>
<conv:EverythingConverterValue ResultValue="Single">
<conv:EverythingConverterValue.ConditionValue>
<sys:Boolean>False</sys:Boolean>
</conv:EverythingConverterValue.ConditionValue>
</conv:EverythingConverterValue>
</conv:EverythingConverter.Conditions>
<conv:EverythingConverter.NullBackValue>
<sys:Boolean>False</sys:Boolean>
</conv:EverythingConverter.NullBackValue>
</conv:EverythingConverter>

- 2,355
- 2
- 32
- 47
I know this is dated, but, you don't need to re-implement anything.
What I did was to negate the value on the property like this:
<!-- XAML code -->
<StackPanel Name="x" Visibility="{Binding Path=Specials, ElementName=MyWindow, Converter={StaticResource BooleanToVisibilityConverter}}"></StackPanel>
<StackPanel Name="y" Visibility="{Binding Path=NotSpecials, ElementName=MyWindow, Converter={StaticResource BooleanToVisibilityConverter}}"></StackPanel>
....
//Code behind
public bool Specials
{
get { return (bool) GetValue(SpecialsProperty); }
set
{
NotSpecials= !value;
SetValue(SpecialsProperty, value);
}
}
public bool NotSpecials
{
get { return (bool) GetValue(NotSpecialsProperty); }
set { SetValue(NotSpecialsProperty, value); }
}
And it works just fine!
Am I missing something?

- 196,001
- 113
- 385
- 569
-
7You think this is an easier solution, and for a single property this might even be the case (it's not reusable for multiple properties, you have to implement it for every one). I feel this is the wrong place for the implementation though, as it has nothing to do with the viewmodel/codeBehind and everything with the view. – Mike Fuchs Mar 06 '13 at 14:26