/Main/src/DynamicDataDisplay/Converters/GenericValueConverter.cs
C# | 62 lines | 44 code | 10 blank | 8 comment | 5 complexity | c52768995c5b434d832f2c13c4249b37 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Windows.Data; 6using System.Globalization; 7 8namespace Microsoft.Research.DynamicDataDisplay.Converters 9{ 10 /// <summary> 11 /// Represents a typed value converter. It simplifies life of its sub-class as there no need to 12 /// check what types arguments have. 13 /// </summary> 14 /// <typeparam name="T"></typeparam> 15 public class GenericValueConverter<T> : IValueConverter 16 { 17 /// <summary> 18 /// Initializes a new instance of the <see cref="GenericValueConverter<T>"/> class. 19 /// </summary> 20 public GenericValueConverter() { } 21 22 private Func<T, object> conversion; 23 public GenericValueConverter(Func<T, object> conversion) 24 { 25 if (conversion == null) 26 throw new ArgumentNullException("conversion"); 27 28 this.conversion = conversion; 29 } 30 31 #region IValueConverter Members 32 33 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 34 { 35 if (value is T) 36 { 37 T genericValue = (T)value; 38 39 object result = ConvertCore(genericValue, targetType, parameter, culture); 40 return result; 41 } 42 return null; 43 } 44 45 public virtual object ConvertCore(T value, Type targetType, object parameter, CultureInfo culture) 46 { 47 if (conversion != null) 48 { 49 return conversion(value); 50 } 51 52 throw new NotImplementedException(); 53 } 54 55 public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 56 { 57 throw new NotSupportedException(); 58 } 59 60 #endregion 61 } 62}