8
8
/// <summary>
9
9
/// Used to convert from four double values to <see cref="CornerRadius"/>
10
10
/// </summary>
11
- public class CornerRadiusConverter : IMultiValueConverter
11
+ [ ValueConversion ( typeof ( CornerRadius ) , typeof ( CornerRadius ) ) ]
12
+ [ ValueConversion ( typeof ( IConvertible [ ] ) , typeof ( CornerRadius ) ) ]
13
+ public class CornerRadiusConverter : IValueConverter , IMultiValueConverter
12
14
{
15
+ /// <inheritdoc />
16
+ public object Convert ( object ? value , Type targetType , object ? parameter , CultureInfo culture )
17
+ {
18
+ var valuesToExtract = parameter is CornerRadiusPart cornerRadiusValue
19
+ ? cornerRadiusValue
20
+ : CornerRadiusPart . All ;
21
+
22
+ var source = ( CornerRadius ) value ! ;
23
+
24
+ var topLeft = valuesToExtract . HasFlag ( CornerRadiusPart . TopLeft ) ? source . TopLeft : 0 ;
25
+ var topRight = valuesToExtract . HasFlag ( CornerRadiusPart . TopRight ) ? source . TopRight : 0 ;
26
+ var bottomRight = valuesToExtract . HasFlag ( CornerRadiusPart . BottomRight ) ? source . BottomRight : 0 ;
27
+ var bottomLeft = valuesToExtract . HasFlag ( CornerRadiusPart . BottomLeft ) ? source . BottomLeft : 0 ;
28
+
29
+ return new CornerRadius ( topLeft , topRight , bottomRight , bottomLeft ) ;
30
+ }
31
+
32
+ /// <inheritdoc />
33
+ public object ConvertBack ( object ? value , Type targetType , object ? parameter , CultureInfo culture )
34
+ {
35
+ throw new NotImplementedException ( ) ;
36
+ }
37
+
13
38
#region Implementation of IMultiValueConverter
14
39
15
40
/// <inheritdoc />
16
41
public object Convert ( object [ ] values , Type targetType , object parameter , CultureInfo culture )
17
42
{
18
- var topLeft = TryConvertSingleValue ( values [ 0 ] ) ;
19
- var topRight = TryConvertSingleValue ( values [ 1 ] ) ;
20
- var bottomRight = TryConvertSingleValue ( values [ 2 ] ) ;
21
- var bottomLeft = TryConvertSingleValue ( values [ 3 ] ) ;
43
+ var valuesToExtract = parameter is CornerRadiusPart cornerRadiusValue
44
+ ? cornerRadiusValue
45
+ : CornerRadiusPart . All ;
46
+
47
+ var topLeft = valuesToExtract . HasFlag ( CornerRadiusPart . TopLeft ) ? TryConvertSingleValue ( values [ 0 ] ) : 0 ;
48
+ var topRight = valuesToExtract . HasFlag ( CornerRadiusPart . TopRight ) ? TryConvertSingleValue ( values [ 1 ] ) : 0 ;
49
+ var bottomRight = valuesToExtract . HasFlag ( CornerRadiusPart . BottomRight ) ? TryConvertSingleValue ( values [ 2 ] ) : 0 ;
50
+ var bottomLeft = valuesToExtract . HasFlag ( CornerRadiusPart . BottomLeft ) ? TryConvertSingleValue ( values [ 3 ] ) : 0 ;
22
51
23
52
return new CornerRadius ( topLeft , topRight , bottomRight , bottomLeft ) ;
24
53
}
@@ -42,4 +71,41 @@ private static double TryConvertSingleValue(object value)
42
71
return 0 ;
43
72
}
44
73
}
74
+ }
75
+
76
+ /// <summary>
77
+ /// Defines parts of a <see cref="CornerRadius"/>.
78
+ /// </summary>
79
+ [ Flags ]
80
+ public enum CornerRadiusPart
81
+ {
82
+ /// <summary>
83
+ /// None.
84
+ /// </summary>
85
+ None = 0 ,
86
+
87
+ /// <summary>
88
+ /// Top left.
89
+ /// </summary>
90
+ TopLeft = 1 << 1 ,
91
+
92
+ /// <summary>
93
+ /// Top right.
94
+ /// </summary>
95
+ TopRight = 1 << 2 ,
96
+
97
+ /// <summary>
98
+ /// Bottom right.
99
+ /// </summary>
100
+ BottomRight = 1 << 3 ,
101
+
102
+ /// <summary>
103
+ /// Bottom left.
104
+ /// </summary>
105
+ BottomLeft = 1 << 4 ,
106
+
107
+ /// <summary>
108
+ /// All parts.
109
+ /// </summary>
110
+ All = TopLeft | TopRight | BottomRight | BottomLeft
45
111
}
0 commit comments