Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Converters/GenericValueConverter.cs @ 13780

Last change on this file since 13780 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 1.6 KB
Line 
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&lt;T&gt;"/> 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}
Note: See TracBrowser for help on using the repository browser.