"binding visibility converter in wpf c#" Code Answer


you have made the classic mistake of binding to auto properties that are valid for binding, but don't notify upon change, which means the binding subsystem cannot detect changes and update the binding targets.

to fix this, implement inotifypropertychanged on your viewmodel, and then ensure that you notify the property change from the properties.

as an example, i have the following in the base class for my viewmodels:

public abstract class baseviewmodel : inotifypropertychanged

    /// <summary>
    /// helper method to set the value of a property and notify if the value has changed.
    /// </summary>
    /// <typeparam name="t"></typeparam>
    /// <param name="newvalue">the value to set the property to.</param>
    /// <param name="currentvalue">the current value of the property.</param>
    /// <param name="notify">flag indicating whether there should be notification if the value has changed.</param>
    /// <param name="notifications">the property names to notify that have been changed.</param>
    protected bool setproperty<t>(ref t newvalue, ref t currentvalue, bool notify, params string[] notifications)
        if (equalitycomparer<t>.default.equals(newvalue, currentvalue))
            return false;

        currentvalue = newvalue;
        if (notify && notifications.length > 0)
            foreach (string propertyname in notifications)

        return true;

    /// <summary>
    /// raises the <see cref="e:propertychanged"/> event.
    /// </summary>
    /// <param name="propertyname">the name of the property that changed.</param>
    protected void onpropertychanged(string propertyname)
        if (this.propertychanged != null)
            this.propertychanged(this, new propertychangedeventargs(propertyname));

    /// <summary>
    /// occurs when a property value changes.
    /// </summary>
    public event propertychangedeventhandler propertychanged;


then in your regular viewmodel:

public class myviewmodel : baseviewmodel
    private bool _countlabelvisible;

    public bool countlabelvisible
        get { return _countlabelvisible; }
        set { setproperty(ref value, ref _countlabelvisible, true, "countlabelvisible", "countlabelvisiblereverse"); }

    public bool countlabelvisiblereverse { get { return !_countlabelvisible; }} 

this way, when countlabelvisible gets changed it also notifies on the property countlabelvisiblereverse, and the property countlabelvisiblereverse consists of only a getter - because it will always be the inverse of countlabelvisible.

so that fixes your code the way you have it, but the reality is you don't need to keep the countlabelvisiblereverse property, instead you could:

  • create an inverse visibility converter as a separate converter
  • create a multi function visibility converter by passing an optional parameter on the binding
  • stack multiple converters, where the output from one converter is piped into the input of the next converter
By mlorbetske on May 9 2022

Answers related to “binding visibility converter in wpf c#”

Only authorized users can answer the Search term. Please sign in first, or register a free account.