55namespace OATControl . Converters
66{
77
8- /// <summary>
9- /// Data binding helper.
10- /// Converts a binding from boolean to an integer value.
11- /// Returns FalseValue when bound value is false and TrueValue when value is ture.
12- /// </summary>
13- public class IntToBoolConverter : MarkupExtension , IValueConverter
14- {
15- /// <summary>
16- /// Gets or sets the integer value to return on the given boolean value.
17- /// </summary>
18- public int TrueValue { get ; set ; }
19- public string Operator { get ; set ; } = "" ;
8+ /// <summary>
9+ /// Data binding helper.
10+ /// Converts a binding from boolean to an integer value.
11+ /// Returns FalseValue when bound value is false and TrueValue when value is ture.
12+ /// </summary>
13+ public class IntToBoolConverter : MarkupExtension , IValueConverter
14+ {
15+ /// <summary>
16+ /// Gets or sets the integer value to return on the given boolean value.
17+ /// </summary>
18+ public string Operator { get ; set ; } = "" ;
2019
21- /// <summary>
22- /// Converts a value.
23- /// </summary>
24- /// <param name="value">The value produced by the binding source.</param>
25- /// <param name="targetType">The type of the binding target property.</param>
26- /// <param name="parameter">The converter parameter to use.</param>
27- /// <param name="culture">The culture to use in the converter.</param>
28- /// <returns>
29- /// A converted value. If the method returns null, the valid null value is used.
30- /// </returns>
31- public object Convert ( object value , Type targetType , object parameter , System . Globalization . CultureInfo culture )
32- {
33- if ( Operator . Equals ( ">" ) )
34- {
35- return ( ( int ) value > this . TrueValue ) ;
36- }
37- if ( Operator . Equals ( ">=" ) )
38- {
39- return ( ( int ) value >= this . TrueValue ) ;
40- }
41- if ( Operator . Equals ( "<" ) )
42- {
43- return ( ( int ) value < this . TrueValue ) ;
44- }
45- if ( Operator . Equals ( "<=" ) )
46- {
47- return ( ( int ) value <= this . TrueValue ) ;
48- }
49- if ( Operator . Equals ( "==" ) )
50- {
51- return ( ( int ) value == this . TrueValue ) ;
52- }
53- if ( Operator . Equals ( "!=" ) )
54- {
55- return ( ( int ) value != this . TrueValue ) ;
56- }
57- return false ;
58- }
20+ /// <summary>
21+ /// Converts a value.
22+ /// </summary>
23+ /// <param name="value">The value produced by the binding source.</param>
24+ /// <param name="targetType">The type of the binding target property.</param>
25+ /// <param name="parameter">The converter parameter to use.</param>
26+ /// <param name="culture">The culture to use in the converter.</param>
27+ /// <returns>
28+ /// A converted value. If the method returns null, the valid null value is used.
29+ /// </returns>
30+ public object Convert ( object value , Type targetType , object parameter , System . Globalization . CultureInfo culture )
31+ {
32+ int . TryParse ( parameter as string , out int paramValue ) ;
33+ if ( Operator . Equals ( ">" ) )
34+ {
35+ return ( ( int ) value > paramValue ) ;
36+ }
37+ if ( Operator . Equals ( ">=" ) )
38+ {
39+ return ( ( int ) value >= paramValue ) ;
40+ }
41+ if ( Operator . Equals ( "<" ) )
42+ {
43+ return ( ( int ) value < paramValue ) ;
44+ }
45+ if ( Operator . Equals ( "<=" ) )
46+ {
47+ return ( ( int ) value <= paramValue ) ;
48+ }
49+ if ( Operator . Equals ( "==" ) )
50+ {
51+ return ( ( int ) value == paramValue ) ;
52+ }
53+ if ( Operator . Equals ( "!=" ) )
54+ {
55+ return ( ( int ) value != paramValue ) ;
56+ }
57+ return false ;
58+ }
5959
60- /// <summary>
61- /// Converts a value back. Not implemented
62- /// </summary>
63- /// <param name="value">The value that is produced by the binding target.</param>
64- /// <param name="targetType">The type to convert to.</param>
65- /// <param name="parameter">The converter parameter to use.</param>
66- /// <param name="culture">The culture to use in the converter.</param>
67- /// <returns>
68- /// A converted value. If the method returns null, the valid null value is used.
69- /// </returns>
70- public object ConvertBack ( object value , Type targetType , object parameter , System . Globalization . CultureInfo culture )
71- {
72- if ( Operator . Equals ( ">" ) )
73- {
74- return this . TrueValue + 1 ;
75- }
76- if ( Operator . Equals ( ">=" ) )
77- {
78- return this . TrueValue + 1 ;
79- }
80- if ( Operator . Equals ( "<" ) )
81- {
82- return this . TrueValue - 1 ;
83- }
84- if ( Operator . Equals ( "<=" ) )
85- {
86- return this . TrueValue - 1 ;
87- }
88- if ( Operator . Equals ( "==" ) )
89- {
90- return this . TrueValue ;
91- }
92- if ( Operator . Equals ( "!=" ) )
93- {
94- return this . TrueValue + 21 ;
95- }
96- return 0 ;
97- }
60+ /// <summary>
61+ /// Converts a value back.
62+ /// </summary>
63+ /// <param name="value">The value that is produced by the binding target.</param>
64+ /// <param name="targetType">The type to convert to.</param>
65+ /// <param name="parameter">The converter parameter to use.</param>
66+ /// <param name="culture">The culture to use in the converter.</param>
67+ /// <returns>
68+ /// A converted value. If the method returns null, the valid null value is used.
69+ /// </returns>
70+ public object ConvertBack ( object value , Type targetType , object parameter , System . Globalization . CultureInfo culture )
71+ {
72+ int . TryParse ( parameter as string , out int paramValue ) ;
73+ if ( Operator . Equals ( ">" ) )
74+ {
75+ return paramValue + 1 ;
76+ }
77+ if ( Operator . Equals ( ">=" ) )
78+ {
79+ return paramValue + 1 ;
80+ }
81+ if ( Operator . Equals ( "<" ) )
82+ {
83+ return paramValue - 1 ;
84+ }
85+ if ( Operator . Equals ( "<=" ) )
86+ {
87+ return paramValue - 1 ;
88+ }
89+ if ( Operator . Equals ( "==" ) )
90+ {
91+ return paramValue ;
92+ }
93+ if ( Operator . Equals ( "!=" ) )
94+ {
95+ return paramValue + 21 ;
96+ }
97+ return 0 ;
98+ }
9899
99- /// <summary>
100- /// When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension.
101- /// </summary>
102- /// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param>
103- /// <returns>
104- /// The object value to set on the property where the extension is applied.
105- /// </returns>
106- public override object ProvideValue ( IServiceProvider serviceProvider )
107- {
108- return this ;
109- }
110- }
100+ /// <summary>
101+ /// When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension.
102+ /// </summary>
103+ /// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param>
104+ /// <returns>
105+ /// The object value to set on the property where the extension is applied.
106+ /// </returns>
107+ public override object ProvideValue ( IServiceProvider serviceProvider )
108+ {
109+ return this ;
110+ }
111+ }
111112}
0 commit comments