-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTwoWayConverterBase.cs
More file actions
62 lines (57 loc) · 2.55 KB
/
TwoWayConverterBase.cs
File metadata and controls
62 lines (57 loc) · 2.55 KB
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
using System.Globalization;
using System.Windows.Data;
namespace BytecodeApi.Wpf;
/// <summary>
/// Represents the base class that wraps the <see cref="IValueConverter" /> and provides simplified wrapper methods for Convert and ConvertBack. This is an abstract class.
/// </summary>
/// <typeparam name="TValue">The type of the value to convert.</typeparam>
public abstract class TwoWayConverterBase<TValue> : ConverterBase<TValue>, IValueConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="TwoWayConverterBase{TValue}" /> class.
/// </summary>
protected TwoWayConverterBase()
{
}
/// <summary>
/// Converts a value back.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <returns>
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
/// </returns>
public abstract object? ConvertBack(object? value);
object? IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
object? result = ConvertBack(value);
return Then == null ? result : Then.Convert(result, targetType, parameter, culture);
}
}
/// <summary>
/// Represents the base class that wraps the <see cref="IValueConverter" /> and provides simplified wrapper methods for Convert and ConvertBack. This is an abstract class.
/// </summary>
/// <typeparam name="TValue">The type of the value to convert.</typeparam>
/// <typeparam name="TParameter">The type of the parameter used for conversion.</typeparam>
public abstract class TwoWayConverterBase<TValue, TParameter> : ConverterBase<TValue, TParameter>, IValueConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="TwoWayConverterBase{TValue, TParameter}" /> class.
/// </summary>
protected TwoWayConverterBase()
{
}
/// <summary>
/// Converts a value back.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <returns>
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
/// </returns>
public abstract object? ConvertBack(object? value, TParameter? parameter);
object? IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
object? result = ConvertBack(value, CSharp.CastOrDefault<TParameter>(parameter));
return Then == null ? result : Then.Convert(result, targetType, parameter, culture);
}
}