1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Windows.Data;
|
---|
6 | using System.Globalization;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|