Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Multiply.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 using System.Windows;
6 using System.Windows.Data;
7 
8 namespace SiliconStudio.Presentation.ValueConverters
9 {
10  /// <summary>
11  /// This converter will multiply a given numeric value by the numeric value given as parameter.
12  /// </summary>
13  [ValueConversion(typeof(double), typeof(double))]
14  public class Multiply : ValueConverterBase<Multiply>
15  {
16  /// <inheritdoc/>
17  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
18  {
19  double scalar;
20  try
21  {
22  scalar = System.Convert.ToDouble(value, culture);
23  }
24  catch (Exception exception)
25  {
26  throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
27  }
28 
29  double param;
30  try
31  {
32  param = System.Convert.ToDouble(parameter, culture);
33  }
34  catch (Exception exception)
35  {
36  throw new ArgumentException("The parameter of this converter must be convertible to a double.", exception);
37  }
38 
39  return System.Convert.ChangeType(scalar * param, targetType);
40  }
41 
42  /// <inheritdoc/>
43  public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
44  {
45  double scalar;
46  try
47  {
48  scalar = System.Convert.ToDouble(value, culture);
49  }
50  catch (Exception exception)
51  {
52  throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
53  }
54 
55  double param;
56  try
57  {
58  param = System.Convert.ToDouble(parameter, culture);
59  }
60  catch (Exception exception)
61  {
62  throw new ArgumentException("The parameter of this converter must be convertible to a double.", exception);
63  }
64 
65  if (Math.Abs(param) > double.Epsilon)
66  {
67  return System.Convert.ChangeType(scalar / param, targetType);
68  }
69 
70  return DependencyProperty.UnsetValue;
71  }
72  }
73 }
override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Definition: Multiply.cs:43
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
Definition: Multiply.cs:17
This converter will multiply a given numeric value by the numeric value given as parameter.
Definition: Multiply.cs:14