-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoolToDoubleConverter.cs
76 lines (72 loc) · 3.29 KB
/
BoolToDoubleConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Globalization;
using System.Windows.Data;
namespace Sharp.Utils.Wpf
{
/// <summary>
/// Converts a boolean to a double precision value.
/// </summary>
/// <remarks>
/// The parameter should be a string representing a number, or 2 numbers separated by a semicolon (;).
/// If the parameter contains 2 numbers, the first number is used as the false value, and the second is used as the true value.
/// If the parameter contains 1 number, it is used as the true value, and an 0 is used as the false value.
/// If the parameter is not specified, then 1 is used as the true value, and an 0 is used as the false value.
/// </remarks>
[ValueConversion(typeof(bool), typeof(double), ParameterType = typeof(string))]
public class BoolToDoubleConverter : IValueConverter
{
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>The converted value.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
throw new ArgumentException("Cannot convert from type " + value.GetType().Name, "value");
double falseValue, trueValue;
ParseParameter(parameter, out falseValue, out trueValue);
return (bool)value ? trueValue : falseValue;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="targetType">The type to convert to.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>The converted value.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is double))
throw new ArgumentException("Cannot convert back from type " + value.GetType().Name, "value");
var doubleValue = (double)value;
double falseValue, trueValue;
ParseParameter(parameter, out falseValue, out trueValue);
return doubleValue == trueValue;
}
private void ParseParameter(object parameter, out double falseValue, out double trueValue)
{
falseValue = 0.0;
trueValue = 1.0;
var stringValue = parameter as string;
if (string.IsNullOrWhiteSpace(stringValue))
return;
var stringValues = stringValue.Split(';');
if (stringValues.Length == 0)
return;
if (stringValues.Length == 1)
{
trueValue = double.Parse(stringValues[0]);
}
else
{
falseValue = double.Parse(stringValues[0]);
trueValue = double.Parse(stringValues[1]);
}
}
}
}