Silveright学习笔记--数据绑定之转换器

     转换器可以将数据从一种类型更改为另一种类型,根据区域性信息转换数据,或者修改表示形式的其他方面。在SL中使用转换器可以实现数据的双向绑定。在前台页面中呈现处理后数据格式,在后台得到我们要存入DB的数据格式。

     转换器要实现System.Windows.Data命名空间下的IValueConverter接口,这个接口有两个方法,Convert和ConvertBack。

View Code
public interface IValueConverter
{
/// <summary>
/// 从绑定源传播给绑定目标
/// </summary>
/// <param name="value">绑定源生成的值</param>
/// <param name="targetType">绑定目标属性的类型</param>
/// <param name="parameter">要使用的转换器参数</param>
/// <param name="culture">要用在转换器中的区域性</param>
/// <returns></returns>
Object Convert(
Object value,
Type targetType,
Object parameter,
CultureInfo culture);

/// <summary>
/// 从绑定目标传播给绑定源
/// </summary>
/// <param name="value">绑定目标生成的值</param>
/// <param name="targetType">要转换到的类型</param>
/// <param name="parameter">要使用的转换器参数</param>
/// <param name="culture">要用在转换器中的区域性</param>
/// <returns></returns>
Object ConvertBack(
Object value,
Type targetType,
Object parameter,
CultureInfo culture);

}

     下面我们为DateTime类型做一个转换器。

DateTimeConverter
1 using System;
2  using System.Globalization;
3 using System.Windows.Data;
4
5 namespace Converter
6 {
7
8 /// <summary>
9 /// It is used to format the date time value to a default format.
10 /// either "dd/MMM/yyyy" or "dd/MMM/yyyy HH:mm"
11 /// </summary>
12 public class DateTimeConverter : IValueConverter
13 {
14
15 /// <summary>
16 /// Modifies the source data before passing it to the target for display in the UI.
17 /// </summary>
18 /// <param name="value">The source data being passed to the target.</param>
19 /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
20 /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
21 /// <param name="culture">The culture of the conversion.</param>
22 /// <returns>
23 /// The value to be passed to the target dependency property.
24 /// </returns>
25 public object Convert(object value,
26 Type targetType,
27 object parameter,
28 CultureInfo culture)
29 {
30 if (value != null)
31 {
32 DateTime date = (DateTime)value;
33 if (!date.Equals(DateTime.MinValue))
34 {
35 if (parameter != null && parameter.ToString().Equals("DateTime"))
36 return date.ToString("dd/MMM/yyyy HH:mm");
37 else if (parameter!=null && parameter.ToString() .Equals("ShortMonth"))
38 return date.ToString("dd/MMM");
41 else
42 return date.ToString("dd/MMM/yyyy");
43 }
44 }
45 return String.Empty;
46 }
47
48
49 /// <summary>
50 /// Modifies the target data before passing it to the source object. This method is called only in
51 /// <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
52 /// </summary>
53 /// <param name="value">The target data being passed to the source.</param>
54 /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
55 /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
56 /// <param name="culture">The culture of the conversion.</param>
57 /// <returns>
58 /// The value to be passed to the source object.
59 /// </returns>
60 public object ConvertBack(object value,
61 Type targetType,
62 object parameter,
63 CultureInfo culture)
64 {
65 if (value==null)
66 return null;
67
68 string strValue = value.ToString();
69 DateTime resultDateTime;
70 if (DateTime.TryParse(strValue, out resultDateTime))
71 {
72 return resultDateTime;
73 }
74 return value;
75 }
76 }
77 }

     接下来在我们要使用这个转换器的页面或者控件的xaml文件中引入转换器的命名空间

View Code
xmlns:ui="clr-namespace:Converter;"

     在<UserControl.Resources>资源中声明转换器

View Code
<UserControl.Resources>
<ui:DateTimeConverter x:Key="Converter" />
</UserControl.Resources>

     最后我们就可以使用这个转换器来显示我们想要的格式了

View Code
Content="{Binding CurrentDate, Converter={StaticResource Converter},ConverterParameter=ShortMonth}"

     这样就可以在页面上显示为 dd/MMM格式的日期了

posted @ 2011-08-13 16:57  hechaner  Views(337)  Comments(0)    收藏  举报