"wpf converter property" Code Answer

1

my booltovisibilityconverter is below. you can either use it as a resource:

 <converters:booltovisibilityconverter x:key="falsetohidden" 
     truevalue="visible" falsevalue="hidden" />

or use it as a markupextension inline in your xaml:

  visibility="{binding myflag, 
      converter={vc:booltovisibilityconverter 
                 falsevalue=collapsed, truevalue=visible}}"

c#

public class booltovisibilityconverter : booltovalueconverter<visibility>
{
    #region constructors and destructors

    public booltovisibilityconverter()
    {
        this.truevalue = visibility.visible;
        this.falsevalue = visibility.collapsed;
    }

    #endregion
}

/// <summary>
/// use as the base class for booltoxxx style converters
/// </summary>
/// <typeparam name="t"></typeparam>    
public abstract class booltovalueconverter<t> : markupextension, ivalueconverter
{
    #region constructors and destructors

    protected booltovalueconverter()
    {
        this.truevalue = default(t);
        this.falsevalue = default(t);
    }

    #endregion

    #region public properties

    public t falsevalue { get; set; }

    public t truevalue { get; set; }

    #endregion

    #region public methods and operators

    public object convert(object value, type targettype, 
                          object parameter, cultureinfo culture)
    {
        return system.convert.toboolean(value) ? this.truevalue : this.falsevalue;
    }

    // override if necessary
    public virtual object convertback(object value, type targettype, 
                                      object parameter, cultureinfo culture)
    {
        return value.equals(this.truevalue);
    }

    public override object providevalue(iserviceprovider serviceprovider)
    {
        return this;
    }

    #endregion
}
By MIP1983 on February 24 2022

Answers related to “wpf converter property”

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