Click here to Skip to main content
13,898,644 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 13 Mar 2008
Licenced CPOL

WPF - Bind to Opposite Boolean Value Using a Converter

, 13 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Learn how to use a value converter for binding to the opposite of a boolean value


A co-worker came to me the other day and asked how she could enable a TextBox when a particular radio button was not selected in WPF. The problem she noted was that she couldn't directly bind the Textbox.IsEnabled property to the RadioButton.IsChecked property. What she really wanted to do was bind IsEnabled to !IsChecked. This article provides an introduction to value converters in WPF using a simple and practical example of a BoolToOppositeBool converter.

Sample Project

The sample scenario for this article is a WPF Windows application with three radio buttons and a TextBox. When the Red radio button is selected, we want the TextBox to be disabled. When any other radio button is selected, we want the TextBox to be enabled. The OppositeBoolConverter discussed in this article is used so that we can leverage data binding.

What are Value Converters?

As the name implies, a value converter converts one type to another. Converters are frequently used in data binding scenarios where the target type and the source type are not the same. Value converters are used heavily in WPF because XAML attributes are strings, but these strings often need to be converted into objects. For instance, the string value of red for a background in XAML needs to be converted to a Color object representing red.

Creating a Custom Converter

To create a custom converter, simply create a class implementing the System.Windows.Data.IValueConverter interface. This interface defines two methods:

  1. Convert - Converts source value to target value
  2. ConvertBack - Converts target value to source value, used for two-way databinding

If you are only using one-way databinding, you only need to implement the Convert method, which is what I have done. In the case of the BoolToOppositeBoolConverter, the Convert implementation returns the opposite boolean value of what was passed in.

public class BoolToOppositeBoolConverter : IValueConverter
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
        if (targetType != typeof(bool))
            throw new InvalidOperationException("The target must be a boolean");

        return !(bool)value;

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
        throw new NotSupportedException();


Using the Converter

Once you have created a custom converter, the next step is to leverage that in data binding by adding the Converter property to a binding expression and specifying an instance of the Converter. In order to implement the scenario of binding a TextBox's IsEnabled property to a radio button's IsChecked property, I first imported the namespace of my Converter into the XAML file:


Next, I created an instance of the Converter as a resource of the Window, with a key of oppositeConverter:

    <wpfdude:BoolToOppositeBoolConverter x:Key="oppositeConverter" />

Finally, I specified the instance of the Converter to use in the binding expression for my TextBox:

<TextBox Height="21" Width="150" Name="textBlock1" Text="Sample Text"

     IsEnabled="{Binding ElementName=radRed, Path=IsChecked,
        Converter={StaticResource oppositeConverter}}" />

That's all that is needed to use a custom converter! The complete XAML for my demo is:

<Window x:Class="OppositeValueConverterDemo.Window1"




 Title="Window1" Height="150" Width="200" >
        <wpfdude:BoolToOppositeBoolConverter x:Key="oppositeConverter" />
    <StackPanel Margin="10,10,10,10">
        <RadioButton IsChecked="True" Height="20" Name="radRed">Red</RadioButton>
        <RadioButton Height="20" Name="redYellow">Yellow</RadioButton>
        <RadioButton Height="20" Name="redGreen">Green</RadioButton>
        <TextBox Height="21" Width="150" Name="textBlock1" Text="Sample Text"

             IsEnabled="{Binding ElementName=radRed, Path=IsChecked,
                Converter={StaticResource oppositeConverter}}" />


The data binding features of WPF minimize the need to write code and value converters extend the scenarios where data binding can be applied. Implementing your own custom value converters can sound intimidating. I hope this article showed that creating one isn't something you need to shy away from.


  • 13th March, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

James J. Foster
Team Leader TJX
United States United States
James J. Foster is a senior systems consultant at The TJX Companies. He currently is leading teams in client development using .NET 3.5, Visual Studio 2008, WPF and WCF.

James holds a Bachelor's degree in Computer Science from WPI and a Master's degree in Software Engineering from Brandeis University. He also is certified as a .NET Developer and SQL Server DBA.

You may also be interested in...

Comments and Discussions

GeneralMy Vote of 5 Pin
Sandeep Singh Shekhawat21-Aug-14 1:11
professionalSandeep Singh Shekhawat21-Aug-14 1:11 
GeneralMy vote of 5 Pin
NotSoDark24-Feb-13 1:52
memberNotSoDark24-Feb-13 1:52 
GeneralThanks! Pin
WhitW6-Dec-12 20:37
memberWhitW6-Dec-12 20:37 
GeneralLocation of IValueConverter Pin
JamesHollowell24-Jul-12 4:20
memberJamesHollowell24-Jul-12 4:20 
GeneralBooleanToggleConverter Pin
Andy Missico5-May-11 16:19
memberAndy Missico5-May-11 16:19 
GeneralMy vote of 5 Pin
LupinTaiwan17-Feb-11 21:12
memberLupinTaiwan17-Feb-11 21:12 
GeneralConvert method possible improvement Pin
JasonHunt17-Sep-10 6:01
memberJasonHunt17-Sep-10 6:01 
Generalnullable bool and Convert back Pin
Erlend Robaye9-Mar-10 1:41
memberErlend Robaye9-Mar-10 1:41 
Generalyou can also use a datatrigger Pin
Leblanc Meneses29-Sep-08 6:07
memberLeblanc Meneses29-Sep-08 6:07 
michellestgeorge13-Mar-08 7:16
membermichellestgeorge13-Mar-08 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 13 Mar 2008
Article Copyright 2008 by James J. Foster
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid