Skip to content

Instantly share code, notes, and snippets.

@stevegreatrex
Created October 4, 2012 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevegreatrex/3835815 to your computer and use it in GitHub Desktop.
Save stevegreatrex/3835815 to your computer and use it in GitHub Desktop.
Reusable Conditional WPF Converter
/// <summary>
/// A converter class used to return conditional values based on the binding value.
/// </summary>
public class When : MarkupExtension, IValueConverter
{
#region Public Properties
/// <summary>
/// Gets or sets the value to which the resolved value will be compared.
/// </summary>
/// <value>
/// The comparison value.
/// </value>
public object IsEqualTo { get; set; }
/// <summary>
/// Gets or sets the object that will be returned if the values match.
/// </summary>
/// <value>
/// The matching return value.
/// </value>
public object Return { get; set; }
/// <summary>
/// Gets or sets the object that will be be returned if the values do not match.
/// </summary>
/// <value>
/// The non-matching return value.
/// </value>
public object Else { get; set; }
#endregion
#region IValueConverter Members
/// <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, CultureInfo culture)
{
if (value == null)
{
if (this.IsEqualTo == null)
return this.Return;
else
return this.Else;
}
if (this.IsEqualTo == null)
return this.Else;
var comparisonValue = GetValueForComparison(value);
if (value.Equals(comparisonValue))
return this.Return;
else
return this.Else;
}
/// <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, CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
#region MarkupExtension Overrides
/// <summary>
/// When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension.
/// </summary>
/// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param>
/// <returns>
/// The object value to set on the property where the extension is applied.
/// </returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
#endregion
#region Private Members
private object GetValueForComparison(object value)
{
var comparisonValue = this.IsEqualTo;
var converter = TypeDescriptor.GetConverter(value.GetType());
try
{
if (converter.CanConvertFrom(this.IsEqualTo.GetType()))
comparisonValue = converter.ConvertFrom(this.IsEqualTo);
}
catch (Exception) {} //ignore failures to convert
return comparisonValue;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment