"wpf imagesource binding with custom converter" Code Answer

5

try to use the switch converter written by josh, should work for you:

switchconverter –

a "switch statement" for xaml - http://josheinstein.com/blog/index.php/2010/06/switchconverter-a-switch-statement-for-xaml/

no need to write your converter, your code will look like this -

<grid.resources>  
    <e:switchconverter x:key="actionicons">  
        <e:switchcase when="security" then="securithimage.png" />  
        <e:switchcase when="structural" then="structureimage.png" />             
    </e:switchconverter>  
</grid.resources>  

<image source="{binding converter={staticresource actionicons}}" />  

update1:

here is code of switchconverter as josh's site seems to be down -

/// <summary>
/// a converter that accepts <see cref="switchconvertercase"/>s and converts them to the 
/// then property of the case.
/// </summary>
[contentproperty("cases")]
public class switchconverter : ivalueconverter
{
    // converter instances.
    list<switchconvertercase> _cases;

    #region public properties.
    /// <summary>
    /// gets or sets an array of <see cref="switchconvertercase"/>s that this converter can use to produde values from.
    /// </summary>
    public list<switchconvertercase> cases { get { return _cases; } set { _cases = value; } }
    #endregion
    #region construction.
    /// <summary>
    /// initializes a new instance of the <see cref="switchconverter"/> class.
    /// </summary>
    public switchconverter()
    {
        // create the cases array.
        _cases = new list<switchconvertercase>();
    }
    #endregion

    /// <summary>
    /// converts a value.
    /// </summary>
    /// <param name="value">the value produced by the binding source.</param>
    /// <param name="targettype">the type of the binding target property.</param>
    /// <param name="parameter">the converter parameter to use.</param>
    /// <param name="culture">the culture to use in the converter.</param>
    /// <returns>
    /// a converted value. if the method returns null, the valid null value is used.
    /// </returns>
    public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)
    {
        // this will be the results of the operation.
        object results = null;

        // i'm only willing to convert switchconvertercases in this converter and no nulls!
        if (value == null) throw new argumentnullexception("value");

        // i need to find out if the case that matches this value actually exists in this converters cases collection.
        if (_cases != null && _cases.count > 0)
            for (int i = 0; i < _cases.count; i++)
            {
                // get a reference to this case.
                switchconvertercase targetcase = _cases[i];

                // check to see if the value is the cases when parameter.
                if (value == targetcase || value.tostring().toupper() == targetcase.when.tostring().toupper())
                {
                    // we've got what we want, the results can now be set to the then property
                    // of the case we're on.
                    results = targetcase.then;

                    // all done, get out of the loop.
                    break;
                }
            }

        // return the results.
        return results;
    }

    /// <summary>
    /// converts a value.
    /// </summary>
    /// <param name="value">the value that is produced by the binding target.</param>
    /// <param name="targettype">the type to convert to.</param>
    /// <param name="parameter">the converter parameter to use.</param>
    /// <param name="culture">the culture to use in the converter.</param>
    /// <returns>
    /// a converted value. if the method returns null, the valid null value is used.
    /// </returns>
    public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)
    {
        throw new notimplementedexception();
    }
}

/// <summary>
/// represents a case for a switch converter.
/// </summary>
[contentproperty("then")]
public class switchconvertercase
{
    // case instances.
    string _when;
    object _then;

    #region public properties.
    /// <summary>
    /// gets or sets the condition of the case.
    /// </summary>
    public string when { get { return _when; } set { _when = value; } }
    /// <summary>
    /// gets or sets the results of this case when run through a <see cref="switchconverter"/>
    /// </summary>
    public object then { get { return _then; } set { _then = value; } }
    #endregion
    #region construction.
    /// <summary>
    /// switches the converter.
    /// </summary>
    public switchconvertercase()
    {
    }
    /// <summary>
    /// initializes a new instance of the <see cref="switchconvertercase"/> class.
    /// </summary>
    /// <param name="when">the condition of the case.</param>
    /// <param name="then">the results of this case when run through a <see cref="switchconverter"/>.</param>
    public switchconvertercase(string when, object then)
    {
        // hook up the instances.
        this._then = then;
        this._when = when;
    }
    #endregion

    /// <summary>
    /// returns a <see cref="system.string"/> that represents this instance.
    /// </summary>
    /// <returns>
    /// a <see cref="system.string"/> that represents this instance.
    /// </returns>
    public override string tostring()
    {
        return string.format("when={0}; then={1}", when.tostring(), then.tostring());
    }
}

update2:

another switchconverter implementation from microsoft reference source.

By fgb on March 2 2022

Answers related to “wpf imagesource binding with custom converter”

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