[转载] Silverlight中使用IValueConverter接口自定义数据源显示方式

2016-08-20 11:05:25来源:http://klq26.blog.51cto.com/2254984/418493作者:klq26人点击


IValueConverter 接口


宋体;color:#666666">提供一种将自定义逻辑应用于绑定的方式。font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">


宋体;color:#666666">命名空间:mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#0033CC">System.Windows.Datafont-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">
宋体;color:#666666">程序集:mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">PresentationFramework(在"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666"> PresentationFramework.dll font-family:宋体;mso-bidi-font-family:宋体;color:#666666">中)"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">"Times New Roman";mso-bidi-font-family:"Times New Roman";color:#666666">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333;mso-no-proof:yes">"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">color:#333333">语法mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">Visual Basicfont-family:宋体;mso-bidi-font-family:宋体;color:#333333">(声明)"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">Publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">Interfacemso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> IValueConverter


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">Visual Basic (font-family:宋体;mso-bidi-font-family:宋体;color:#333333">用法"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">)


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">Dimmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> instance mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">Asmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> IValueConverter


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">C#


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> interface IValueConverter


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">Visual C++


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> interface "inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
"Courier New";color:blue">class"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
"Courier New";color:#333333"> IValueConverter


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">J#


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> interface IValueConverter


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">JScript


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:blue">publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"> interface IValueConverter


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">XAML


mso-bidi-font-family:宋体;color:#333333">可以直接在font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:#333333"> XAML color:#333333">中使用接口,请参见实现该接口的类型。font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:#333333">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Times New Roman"">


"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">color:#333333">备注mso-bidi-font-family:宋体;color:#333333">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">


宋体;color:#666666">如果要将值转换器与绑定关联,请创建一个实现font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">IValueConvertercolor:#666666">接口的类,然后实现mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#0033CC">Convertmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">font-family:宋体;mso-bidi-font-family:宋体;color:#666666">和"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">"Times New Roman";mso-bidi-font-family:Tahoma;color:#0033CC">ConvertBack"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">方法。转换器可以将数据从一种类型更改为另一种类型,根据区域性信息转换数据,或修改表示形式的其他方面。"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">


宋体;color:#666666">值转换器是分区域性的。"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#0033CC">Convert"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#666666">font-family:宋体;mso-bidi-font-family:宋体;color:#666666">和"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">"Times New Roman";mso-bidi-font-family:Tahoma;color:#0033CC">ConvertBack"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">方法都具有指示区域性信息的"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#666666">"Segoe UI","sans-serif";mso-fareast-font-family:"Times New Roman";color:#666666">culture"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">参数。如果区域性信息与转换无关,那么在自定义转换器中可以忽略该参数。10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">


"Times New Roman";mso-bidi-font-family:Tahoma;color:#0033CC">Convert"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">和mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#0033CC">ConvertBack"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#666666">font-family:宋体;mso-bidi-font-family:宋体;color:#666666">方法还具有名为"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">"Times New Roman";color:#666666">parameterfont-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">的参数,以便您能将转换器的同一个实例与不同参数一起使用。例如,您可以编写格式设置转换器,以根据所用的输入参数生成不同的数据格式。可以使用"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">"Times New Roman";mso-bidi-font-family:Tahoma;color:purple">Binding"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">类的mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#0033CC">ConverterParameterfont-family:宋体;mso-bidi-font-family:宋体;color:#666666">,将参数作为变量传递给"Times New Roman";mso-bidi-font-family:Tahoma;color:#0033CC">Convert"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">和mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#0033CC">ConvertBack"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#666666">font-family:宋体;mso-bidi-font-family:宋体;color:#666666">方法。"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">




mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333;mso-no-proof:yes">"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">color:#333333">示例mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">


宋体;color:#666666">本示例演示如何将转换应用于绑定中使用的数据。font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">


宋体;color:#666666">要在绑定期间转换数据,必须创建一个实现font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">IValueConvertercolor:#666666">接口的类,其中包括mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#0033CC">Convertfont-family:宋体;mso-bidi-font-family:宋体;color:#666666">和"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">"Times New Roman";mso-bidi-font-family:Tahoma;color:#0033CC">ConvertBack"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">方法。mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">


宋体;color:#666666">下面的示例演示一个日期转换器的实现,此日期转换器转换传入的日期值,使其只显示年月日。实现"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">IValueConvertercolor:#666666">接口时,最好用mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#0033CC">ValueConversionAttributefont-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#666666">属性来修饰此实现,以便向开发工具指示转换所涉及的数据类型,如下面的示例所示:"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">C#


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">[ValueConversion(typeof(DateTime), typeof(String))]
"Times New Roman";mso-bidi-font-family:Tahoma;color:blue">public"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">"Times New Roman";mso-bidi-font-family:Tahoma;color:blue">class"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">DateConverter : IValueConverter
{
mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:blue">publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:blue">return10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#333333">date.ToShortDateString();
}

mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:blue">publicmso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:blue">string10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#333333">strValue = value.ToString();
DateTime resultDateTime;
10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:blue">iffont-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#333333">(DateTime.TryParse(strValue, out resultDateTime))
{
"Times New Roman";mso-bidi-font-family:Tahoma;color:blue">return"Times New Roman";mso-bidi-font-family:Tahoma;color:#333333">resultDateTime;
}
10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:blue">return10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#333333">value;
}
}


宋体;color:#666666">一旦创建了转换器,即可将其作为一项资源添加到可扩展应用程序标记语言"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666"> (XAML) color:#666666">文件中。在下面的示例中,"inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
Tahoma;color:#666666">srcfont-family:宋体;mso-bidi-font-family:宋体;color:#666666">映射到在其中定义"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">DateConvertercolor:#666666">的命名空间。mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">C#


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"><src:DateConverter x:Key=font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:maroon">"dateConverter""Times New Roman";mso-bidi-font-family:"Courier New";color:#333333">/>


宋体;color:#666666">最后,通过以下语法在绑定中使用转换器。在下面的示例中,10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Tahoma;color:#0033CC">TextBlock"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">color:#666666">的文本内容绑定到mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#666666">StartDatefont-family:宋体;mso-bidi-font-family:宋体;color:#666666">,后者是外部数据源的一个属性。"Times New Roman";mso-bidi-font-family:Tahoma;color:#666666">


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:Tahoma;
color:#333333">C#


mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333"><TextBlock Grid.Row=font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:maroon">"2""Times New Roman";mso-bidi-font-family:"Courier New";color:#333333"> Grid.Column=mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:maroon">"0""inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
"Courier New";color:#333333"> Margin=font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:maroon">"0,0,8,0""Times New Roman";mso-bidi-font-family:"Courier New";color:#333333">
Name="Times New Roman";mso-bidi-font-family:"Courier New";color:maroon">"startDateTitle""Times New Roman";mso-bidi-font-family:"Courier New";color:#333333">
Style="Times New Roman";mso-bidi-font-family:"Courier New";color:maroon">"{StaticResource smallTitleStyle}"mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333">>Start Date:</TextBlock>
<TextBlock Name=mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:maroon">"StartDateDTKey"font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:#333333"> Grid.Row="Times New Roman";mso-bidi-font-family:"Courier New";color:maroon">"2""Times New Roman";mso-bidi-font-family:"Courier New";color:#333333"> Grid.Column=mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:maroon">"1""inherit","serif";mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:
"Courier New";color:#333333">
Text=10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:maroon">"{Binding Path=StartDate, Converter={StaticResource dateConverter}}"10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:#333333">
Style=10.5pt;font-family:"inherit","serif";mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:"Courier New";color:maroon">"{StaticResource textStyleTextBlock}"mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Courier New";
color:#333333">/>



原文地址:http://www.cnthub.com/page/M0/S207/207753.html


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台