翼度科技»论坛 编程开发 .net 查看内容

4.Binding类之转化器和验证规则

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
转化器
IValueConverter接口
Binding类还有一个Converter属性,其实,它是一个IValueConverter接口。它的主要作用是:前后端建立绑定时,定义一套自定义逻辑,让前端显示的数据与后端获取的数据建立一定的对应关系。
比如Person对象有一个年龄(Age)属性,我们在前端显示某个人的年龄时,可以根据不同的年龄,显示不同的背景颜色。这个时候,实际上是根据这个输入的整型数据返回一个不同颜色的画刷。
  1. <Window.Resources>
  2.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  3. </Window.Resources>//
  4. <Window.Resources>
  5.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  6. </Window.Resources>// 摘要:
  7. <Window.Resources>
  8.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  9. </Window.Resources>//<Window.Resources>
  10.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  11. </Window.Resources> Provides a way to apply custom logic to a binding.
  12. <Window.Resources>
  13.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  14. </Window.Resources>public interface IValueConverter
  15. <Window.Resources>
  16.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  17. </Window.Resources>{
  18. <Window.Resources>
  19.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  20. </Window.Resources><Window.Resources>
  21.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  22. </Window.Resources>object Convert(object value, Type targetType, object parameter, CultureInfo culture);
  23. <Window.Resources>
  24.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  25. </Window.Resources><Window.Resources>
  26.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  27. </Window.Resources>
  28. <Window.Resources>
  29.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  30. </Window.Resources><Window.Resources>
  31.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  32. </Window.Resources>object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
  33. <Window.Resources>
  34.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  35. </Window.Resources>}
复制代码
 
IValueConverter接口有两个方法成员,分别是Convert和ConvertBack。
Convert方法成员:输入的value及parameter参数,根据自定义逻辑判断,返回一个object对象给前端XAML使用。
ConvertBack方法成员:与Convert相反,将前端输入的数据转换成另一个对象返回给后端的数据源。
IValueConverter示例
  1. public class AgeToColorConverter : IValueConverter
  2. {
  3. <Window.Resources>
  4.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  5. </Window.Resources>public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  6. <Window.Resources>
  7.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  8. </Window.Resources>{
  9. <Window.Resources>
  10.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  11. </Window.Resources><Window.Resources>
  12.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  13. </Window.Resources>SolidColorBrush background = Brushes.Black;
  14. <Window.Resources>
  15.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  16. </Window.Resources><Window.Resources>
  17.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  18. </Window.Resources>if (value != null && int.TryParse(value.ToString(), out int age))
  19. <Window.Resources>
  20.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  21. </Window.Resources><Window.Resources>
  22.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  23. </Window.Resources>{
  24. <Window.Resources>
  25.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  26. </Window.Resources><Window.Resources>
  27.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  28. </Window.Resources><Window.Resources>
  29.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  30. </Window.Resources>if (age < 20)
  31. <Window.Resources>
  32.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  33. </Window.Resources><Window.Resources>
  34.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  35. </Window.Resources><Window.Resources>
  36.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  37. </Window.Resources>{
  38. <Window.Resources>
  39.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  40. </Window.Resources><Window.Resources>
  41.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  42. </Window.Resources><Window.Resources>
  43.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  44. </Window.Resources><Window.Resources>
  45.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  46. </Window.Resources>background = Brushes.Green;
  47. <Window.Resources>
  48.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  49. </Window.Resources><Window.Resources>
  50.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  51. </Window.Resources><Window.Resources>
  52.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  53. </Window.Resources>}
  54. <Window.Resources>
  55.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  56. </Window.Resources><Window.Resources>
  57.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  58. </Window.Resources><Window.Resources>
  59.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  60. </Window.Resources>else if (age < 40)
  61. <Window.Resources>
  62.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  63. </Window.Resources><Window.Resources>
  64.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  65. </Window.Resources><Window.Resources>
  66.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  67. </Window.Resources>{
  68. <Window.Resources>
  69.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  70. </Window.Resources><Window.Resources>
  71.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  72. </Window.Resources><Window.Resources>
  73.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  74. </Window.Resources><Window.Resources>
  75.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  76. </Window.Resources>background = Brushes.Blue;
  77. <Window.Resources>
  78.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  79. </Window.Resources><Window.Resources>
  80.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  81. </Window.Resources><Window.Resources>
  82.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  83. </Window.Resources>}
  84. <Window.Resources>
  85.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  86. </Window.Resources><Window.Resources>
  87.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  88. </Window.Resources><Window.Resources>
  89.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  90. </Window.Resources>else if (age < 60)
  91. <Window.Resources>
  92.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  93. </Window.Resources><Window.Resources>
  94.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  95. </Window.Resources><Window.Resources>
  96.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  97. </Window.Resources>{
  98. <Window.Resources>
  99.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  100. </Window.Resources><Window.Resources>
  101.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  102. </Window.Resources><Window.Resources>
  103.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  104. </Window.Resources><Window.Resources>
  105.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  106. </Window.Resources>background = Brushes.Orange;
  107. <Window.Resources>
  108.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  109. </Window.Resources><Window.Resources>
  110.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  111. </Window.Resources><Window.Resources>
  112.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  113. </Window.Resources>}
  114. <Window.Resources>
  115.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  116. </Window.Resources><Window.Resources>
  117.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  118. </Window.Resources><Window.Resources>
  119.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  120. </Window.Resources>else if (age < 80)
  121. <Window.Resources>
  122.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  123. </Window.Resources><Window.Resources>
  124.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  125. </Window.Resources><Window.Resources>
  126.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  127. </Window.Resources>{
  128. <Window.Resources>
  129.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  130. </Window.Resources><Window.Resources>
  131.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  132. </Window.Resources><Window.Resources>
  133.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  134. </Window.Resources><Window.Resources>
  135.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  136. </Window.Resources>background = Brushes.Red;
  137. <Window.Resources>
  138.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  139. </Window.Resources><Window.Resources>
  140.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  141. </Window.Resources><Window.Resources>
  142.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  143. </Window.Resources>}
  144. <Window.Resources>
  145.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  146. </Window.Resources><Window.Resources>
  147.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  148. </Window.Resources><Window.Resources>
  149.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  150. </Window.Resources>else if (age < 90)
  151. <Window.Resources>
  152.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  153. </Window.Resources><Window.Resources>
  154.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  155. </Window.Resources><Window.Resources>
  156.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  157. </Window.Resources>{
  158. <Window.Resources>
  159.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  160. </Window.Resources><Window.Resources>
  161.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  162. </Window.Resources><Window.Resources>
  163.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  164. </Window.Resources><Window.Resources>
  165.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  166. </Window.Resources>background = Brushes.Purple;
  167. <Window.Resources>
  168.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  169. </Window.Resources><Window.Resources>
  170.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  171. </Window.Resources><Window.Resources>
  172.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  173. </Window.Resources>}
  174. <Window.Resources>
  175.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  176. </Window.Resources><Window.Resources>
  177.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  178. </Window.Resources><Window.Resources>
  179.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  180. </Window.Resources>else
  181. <Window.Resources>
  182.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  183. </Window.Resources><Window.Resources>
  184.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  185. </Window.Resources><Window.Resources>
  186.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  187. </Window.Resources>{
  188. <Window.Resources>
  189.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  190. </Window.Resources><Window.Resources>
  191.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  192. </Window.Resources><Window.Resources>
  193.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  194. </Window.Resources><Window.Resources>
  195.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  196. </Window.Resources>background = Brushes.Gray;
  197. <Window.Resources>
  198.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  199. </Window.Resources><Window.Resources>
  200.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  201. </Window.Resources><Window.Resources>
  202.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  203. </Window.Resources>}
  204. <Window.Resources>
  205.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  206. </Window.Resources><Window.Resources>
  207.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  208. </Window.Resources>}
  209. <Window.Resources>
  210.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  211. </Window.Resources><Window.Resources>
  212.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  213. </Window.Resources>return background;
  214. <Window.Resources>
  215.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  216. </Window.Resources>}
  217. <Window.Resources>
  218.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  219. </Window.Resources>public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  220. <Window.Resources>
  221.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  222. </Window.Resources>{
  223. <Window.Resources>
  224.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  225. </Window.Resources><Window.Resources>
  226.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  227. </Window.Resources>throw new NotImplementedException();
  228. <Window.Resources>
  229.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  230. </Window.Resources>}
  231. }
复制代码
然后在XAML前端对转换器进行实例化
  1. <Window.Resources>
  2.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  3. </Window.Resources>
复制代码
我们就可以使用这个Key名叫AgeToColorConverter的实例。
  1. Background="{Binding Person.Age,Converter={StaticResource AgeToColorConverter}}"
复制代码
 
IMultiValueConverter接口
与IValueConverter类似的,还有一个叫IMultiValueConverter——多值转换器。它的定义和IValueConverter也十分类似。
  1. public interface IMultiValueConverter{<Window.Resources>
  2.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  3. </Window.Resources>object Convert(object[] values, Type targetType, object parameter, CultureInfo culture);<Window.Resources>
  4.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  5. </Window.Resources>object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture); }
复制代码
Convert第一个参数变成了values,表示它可以传入多个值。
IMultiValueConverter示例
  1. ///  /// 多值转换器 ///  public class MultiColorConverter : IMultiValueConverter {<Window.Resources>
  2.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  3. </Window.Resources> public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)<Window.Resources>
  4.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  5. </Window.Resources> {<Window.Resources>
  6.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  7. </Window.Resources><Window.Resources>
  8.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  9. </Window.Resources> if (values != null && values.Length == 2)<Window.Resources>
  10.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  11. </Window.Resources><Window.Resources>
  12.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  13. </Window.Resources> {<Window.Resources>
  14.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  15. </Window.Resources><Window.Resources>
  16.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  17. </Window.Resources><Window.Resources>
  18.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  19. </Window.Resources> var age_result = int.TryParse(values[0].ToString(), out int age);<Window.Resources>
  20.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  21. </Window.Resources><Window.Resources>
  22.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  23. </Window.Resources><Window.Resources>
  24.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  25. </Window.Resources> var money_result = int.TryParse(values[1].ToString(), out int money);<Window.Resources>
  26.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  27. </Window.Resources><Window.Resources>
  28.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  29. </Window.Resources><Window.Resources>
  30.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  31. </Window.Resources> if(age_result&& money_result)<Window.Resources>
  32.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  33. </Window.Resources><Window.Resources>
  34.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  35. </Window.Resources><Window.Resources>
  36.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  37. </Window.Resources> {<Window.Resources>
  38.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  39. </Window.Resources><Window.Resources>
  40.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  41. </Window.Resources><Window.Resources>
  42.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  43. </Window.Resources><Window.Resources>
  44.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  45. </Window.Resources> if (age < 30 && money > 50000)<Window.Resources>
  46.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  47. </Window.Resources><Window.Resources>
  48.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  49. </Window.Resources><Window.Resources>
  50.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  51. </Window.Resources><Window.Resources>
  52.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  53. </Window.Resources> {<Window.Resources>
  54.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  55. </Window.Resources><Window.Resources>
  56.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  57. </Window.Resources><Window.Resources>
  58.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  59. </Window.Resources><Window.Resources>
  60.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  61. </Window.Resources><Window.Resources>
  62.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  63. </Window.Resources> return "年纪轻轻的有钱人";<Window.Resources>
  64.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  65. </Window.Resources><Window.Resources>
  66.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  67. </Window.Resources><Window.Resources>
  68.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  69. </Window.Resources><Window.Resources>
  70.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  71. </Window.Resources> }<Window.Resources>
  72.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  73. </Window.Resources><Window.Resources>
  74.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  75. </Window.Resources><Window.Resources>
  76.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  77. </Window.Resources><Window.Resources>
  78.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  79. </Window.Resources> else if (age >= 30 && age = 30 && money > 90000)<Window.Resources>
  80.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  81. </Window.Resources><Window.Resources>
  82.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  83. </Window.Resources><Window.Resources>
  84.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  85. </Window.Resources><Window.Resources>
  86.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  87. </Window.Resources> {<Window.Resources>
  88.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  89. </Window.Resources><Window.Resources>
  90.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  91. </Window.Resources><Window.Resources>
  92.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  93. </Window.Resources><Window.Resources>
  94.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  95. </Window.Resources><Window.Resources>
  96.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  97. </Window.Resources> return "富豪";<Window.Resources>
  98.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  99. </Window.Resources><Window.Resources>
  100.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  101. </Window.Resources><Window.Resources>
  102.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  103. </Window.Resources><Window.Resources>
  104.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  105. </Window.Resources> }<Window.Resources>
  106.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  107. </Window.Resources><Window.Resources>
  108.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  109. </Window.Resources><Window.Resources>
  110.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  111. </Window.Resources><Window.Resources>
  112.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  113. </Window.Resources> else<Window.Resources>
  114.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  115. </Window.Resources><Window.Resources>
  116.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  117. </Window.Resources><Window.Resources>
  118.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  119. </Window.Resources><Window.Resources>
  120.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  121. </Window.Resources> {<Window.Resources>
  122.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  123. </Window.Resources><Window.Resources>
  124.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  125. </Window.Resources><Window.Resources>
  126.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  127. </Window.Resources><Window.Resources>
  128.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  129. </Window.Resources><Window.Resources>
  130.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  131. </Window.Resources> return "一个平凡的人";<Window.Resources>
  132.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  133. </Window.Resources><Window.Resources>
  134.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  135. </Window.Resources><Window.Resources>
  136.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  137. </Window.Resources><Window.Resources>
  138.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  139. </Window.Resources> }<Window.Resources>
  140.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  141. </Window.Resources><Window.Resources>
  142.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  143. </Window.Resources><Window.Resources>
  144.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  145. </Window.Resources> }<Window.Resources>
  146.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  147. </Window.Resources><Window.Resources>
  148.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  149. </Window.Resources><Window.Resources>
  150.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  151. </Window.Resources><Window.Resources>
  152.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  153. </Window.Resources><Window.Resources>
  154.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  155. </Window.Resources>  }<Window.Resources>
  156.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  157. </Window.Resources><Window.Resources>
  158.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  159. </Window.Resources> return null;<Window.Resources>
  160.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  161. </Window.Resources> }<Window.Resources>
  162.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  163. </Window.Resources>  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)<Window.Resources>
  164.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  165. </Window.Resources> {<Window.Resources>
  166.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  167. </Window.Resources><Window.Resources>
  168.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  169. </Window.Resources> throw new NotImplementedException();<Window.Resources>
  170.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  171. </Window.Resources> } }
复制代码
如上所示,我们定义了一个多值转换器,values参数传入了两个元素,分别是年龄和金钱。这里为什么能确定是两个元素?因为我们在前端使用这个转换器时,明确的传入了两个值。
xaml前端页面使用:
  1. <Window.Resources>
  2.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  3. </Window.Resources><Window.Resources>
  4.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  5. </Window.Resources><Window.Resources>
  6.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  7. </Window.Resources><Window.Resources>
  8.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  9. </Window.Resources><Window.Resources>
  10.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  11. </Window.Resources><Window.Resources>
  12.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  13. </Window.Resources><Window.Resources>
  14.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  15. </Window.Resources><Window.Resources>
  16.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  17. </Window.Resources><Window.Resources>
  18.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  19. </Window.Resources><Window.Resources>
  20.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  21. </Window.Resources><Window.Resources>
  22.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  23. </Window.Resources><Window.Resources>
  24.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  25. </Window.Resources><Window.Resources>
  26.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  27. </Window.Resources><Window.Resources>
  28.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  29. </Window.Resources><Window.Resources>
  30.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  31. </Window.Resources><Window.Resources>
  32.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  33. </Window.Resources><Window.Resources>
  34.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  35. </Window.Resources><Window.Resources>
  36.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  37. </Window.Resources><Window.Resources>
  38.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  39. </Window.Resources><Window.Resources>
  40.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  41. </Window.Resources><Window.Resources>
  42.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  43. </Window.Resources><Window.Resources>
  44.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  45. </Window.Resources><Window.Resources>
  46.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  47. </Window.Resources><Window.Resources>
  48.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  49. </Window.Resources><Window.Resources>
  50.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  51. </Window.Resources><Window.Resources>
  52.     <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
  53. </Window.Resources>
复制代码
这里需要着重的讲解一下多值转换器在前端的使用。MultiBinding和IMultiValueConverter通常是配套使用的。MultiBinding表示多路绑定的意思,和Binding的用法类似,只是多了一个Bindings集合——表示拥有多个绑定源。
 
验证规则
ValidationRule是一个抽象类,提供创建自定义规则的一个方式,旨在检查用户输入的有效性。所以,我们要验证前端输入的各项数据的有效性时,需要自己定义各自的验证规则。
在数据绑定时,Binding类有一个ValidationRules属性,这个属性专门用来存放开发者自定义的验证规则。
例如,我们假定用户名的长度必须在1-10个字符之间,且用户的年龄在1-100之前,那么就可以围绕这两个条件自定义两个不同的验证规则,它们定义如下:
用户名验证规则:
[code]public class NameValidationRule : ValidationRule{<Window.Resources>
    <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
</Window.Resources>public override ValidationResult Validate(object value, CultureInfo cultureInfo)<Window.Resources>
    <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
</Window.Resources>{<Window.Resources>
    <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
</Window.Resources><Window.Resources>
    <local:AgeToColorConverter x:Key="AgeToColorConverter"/>
</Window.Resources>if (value != null && value.ToString().Length > 1 && value.ToString().Length = 1 && myValue

上一篇: 6.Trigger触发器

下一篇: 5.WPF样式Style

举报 回复 使用道具