/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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Data;
- using System.Globalization;
-
- namespace Microsoft.Research.DynamicDataDisplay.Converters
- {
- /// <summary>
- /// Represents a typed value converter. It simplifies life of its sub-class as there no need to
- /// check what types arguments have.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class GenericValueConverter<T> : IValueConverter
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="GenericValueConverter<T>"/> class.
- /// </summary>
- public GenericValueConverter() { }
-
- private Func<T, object> conversion;
- public GenericValueConverter(Func<T, object> conversion)
- {
- if (conversion == null)
- throw new ArgumentNullException("conversion");
-
- this.conversion = conversion;
- }
-
- #region IValueConverter Members
-
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is T)
- {
- T genericValue = (T)value;
-
- object result = ConvertCore(genericValue, targetType, parameter, culture);
- return result;
- }
- return null;
- }
-
- public virtual object ConvertCore(T value, Type targetType, object parameter, CultureInfo culture)
- {
- if (conversion != null)
- {
- return conversion(value);
- }
-
- throw new NotImplementedException();
- }
-
- public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotSupportedException();
- }
-
- #endregion
- }
- }