Setting Visibility of WPF Control through Binding

Posted on

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 thoughts on “Setting Visibility of WPF Control through Binding

    Ryan said:
    October 16, 2009 at 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 responded:
      October 16, 2009 at 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.

    Mayrun Digmi said:
    April 8, 2010 at 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

    Bembeng Arifin responded:
    June 4, 2010 at 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)

    darnkitty said:
    March 8, 2011 at 1:38 pm

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

    Artiom Feoktistov said:
    June 28, 2011 at 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;

    Goran Zadro said:
    July 8, 2011 at 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

    […] buona guida spiega come dichiararlo ed utilizzarlo. I passi sono i […]

    rhyous said:
    August 30, 2011 at 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.

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 )

Google+ photo

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


Connecting to %s