首页 > 代码库 > wpf值转换器IValueConverter例子

wpf值转换器IValueConverter例子

转载:http://blog.163.com/wangzhenguo2005@126/blog/static/37140526201085113430862/

值转换器可以把一种类型转换成另一种类型。例如,绑定到一个代表图片地址的字符串,希望显示的是图片,将数据存储为浮点类型,但通过货币的形式呈现;还有将日期存储成DateTime格式,在界面上显示时使用Calender控件等。
下面写一个简单的例子,获得系统当前的时间,显示”now is 2010-xx-xx xx:xx;xx”。
xaml的代码:

 1   2 <Window x:Class="VelueConverterTest.Window1" 3    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5        xmlns:local="clr-namespace:VelueConverterTest" 6    Title="Window1" Height="300" Width="300"> 7     <Window.Resources> 8         <local:DateConverter x:Key="dateConverter"/> 9     </Window.Resources>10     <Grid>11         <Label Margin="53,104,45,130" Name="label1" Content="{Binding Converter={StaticResource dateConverter}}"/>12     </Grid>13 </Window>

 

XAML文件定义了一个dateConverter资源。指向CS文件中的DateConverter类。

 
  1 1  2 2  3 3  4 4  5 5  6 6  7 7  8 8  9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 using System; 52 using System.Collections.Generic; 53 using System.Linq; 54 using System.Text; 55 using System.Windows; 56 using System.Windows.Controls; 57 using System.Windows.Data; 58 using System.Windows.Documents; 59 using System.Windows.Input; 60 using System.Windows.Media; 61 using System.Windows.Media.Imaging; 62 using System.Windows.Navigation; 63 using System.Windows.Shapes; 64 using System.Globalization; 65  66 namespace VelueConverterTest 67 { 68     public partial class Window1 : Window 69     { 70         public DateTime nowtime { get; set; } 71         public Window1() 72         { 73             InitializeComponent(); 74             nowtime = DateTime.Now; 75             label1.DataContext = nowtime; 76         } 77     } 78  79    //定义值转换器 80     [ValueConversion(typeof(DateTime), typeof(String))] 81     public class DateConverter : IValueConverter 82     { 83         public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 84         { 85             DateTime date = (DateTime)value; 86             return "now is "+date.ToString(); 87         } 88  89         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 90         { 91             string strValue =http://www.mamicode.com/ value.ToString(); 92             DateTime resultDateTime; 93             if (DateTime.TryParse(strValue, out resultDateTime)) 94             { 95                 return resultDateTime; 96             } 97             return value; 98         } 99     }100 }

 

Convert和ConvertBack的区别:
Convert函数表示从数据源到目标的值转换,ConvertBack函数表示从目标到数据源的值转换。因此,如果绑定模式是一次绑定或单向 绑定,只需实现Convert函数;如果绑定模式是双向绑定,需要实现Convert和ConvertBack函数。

xaml中定义了label的Converter,当执行绑定的时候,WPF会把转换前的值,如本例中的nowtime 做为转换器函数Convert的输入值,将返回值显示在label控件上。

wpf值转换器IValueConverter例子