Skip to content

Setting Visibility of WPF Control through Binding

August 12, 2009

Just found out that the Visibility property of WPF controls is not having true / false value.
So just by returning bool (IsControlVisible) like the code below won’t work:

<ContentControl Visibility="{Binding Path=IsControlVisible}"></ContentControl>


That’s because Visibility is an enumeration You will need to use a converter to convert the bool into the enum. Fortunately there is a BooleanToVisibilityConverter available so we need not create own custom converter.

Include this into the resources.

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
And use it in the control binding
<ContentControl Visibility="{Binding Path=IsControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"></ContentControl>


Hope this helps ;) 
10 Comments leave one →
  1. Ryan permalink
    October 16, 2009 1:09 pm

    Thanks for this. Saved me writing my own Converter.

    Although I still have to write a custom converter when I want a control to be visible when the data value is false, i.e. reversed.

    • Bembeng Arifin permalink*
      October 16, 2009 1:37 pm

      Hi Ryan,
      The reason that I use the property name as IsControlVisible in the code snippet, is basically just to control the visibility of the Control.
      The decision to hide or to display the control, as for example the data value is false can be performed separately.
      Another easy way is probably to negate the condition, e.g.

      Public bool SomeDataValue { get; set; }

      Public bool IsControlVisible
      get { return !SomeDataValue; }

      Hope this helps.

  2. Mayrun Digmi permalink
    April 8, 2010 8:52 am

    You’re example is not working at all.

    You need to add ElementName.

    Good Example:

    (Assosicate to control)

    Visibility=”{Binding ElementName=MyBlaBlaName, Path=IsControlVisible, Converter={StaticResource myBooleanToVisibilityConverter}}”

    Mayrun Digmi

  3. Bembeng Arifin permalink*
    June 4, 2010 6:40 am

    Hi Mayrun,

    I’m using ViewModel on this example, so IsControlVisible is the property of the ViewModel therefore it’s referring to the current View DataContext (ViewModel)

  4. March 8, 2011 1:38 pm

    Fantastic – worked for me just as shown (with no need to set ElementName as I’m using ViewModel as well)

  5. June 28, 2011 12:42 pm

    Maybe it’s very conceptually wrong, but without using a Model it would be:


    public partial class MainWindow : Window
    public MainWindow()
    this.DataContext = this;

    static DependencyProperty IsControlVisibleProperty = DependencyProperty.Register(“IsControlVisible”, typeof(bool), typeof(MainWindow));

    public bool IsControlVisible

    get { return (bool)this.GetValue(IsControlVisibleProperty); }

    this.SetValue(IsControlVisibleProperty, value);


    private void button3_Click(object sender, RoutedEventArgs e)
    IsControlVisible = true;

    private void button4_Click(object sender, RoutedEventArgs e)
    IsControlVisible = false;

  6. July 8, 2011 4:23 pm

    This will not work – You need to have Property Visibility in View Model.
    Visibility _loginScreen;

    public Visibility LoginScreen
    return _loginScreen;
    if (_isLogged == false)
    _loginScreen = Visibility.Visible;
    _loginScreen = Visibility.Collapsed;



    Now you can bind this and it is going to work

  7. August 30, 2011 4:21 am

    I wrote a post that extends this post with more information.

    Binding Visibility to a bool value in WPF

    Hope it helps out readers who need a few more examples and need to do a little more customization.


  1. Converter e Visibilità in WPF « Desktop & Mobile
  2. (Solved) c# Image name as string, use it to make image visible WPF | PHP Developer Resource

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: