WPF Binding值转换器ValueConverter使用简介(一)
来源:互联网 发布:装修公司源码 编辑:程序博客网 时间:2024/06/05 19:03
WPF、Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异的,此时我们就需要定制自己的Converter。
.Net Framework提供了两种Converter接口,单值转换的接口IValueConverter和多值转换的接口IMultiValueConverter,它们都属于System.Windows.Data命名空间,在程序集PresentationFramework.dll中。这两种值转换器都是分区域性的。其中方法Convert和ConvertBack都具有指示区域性信息的culture参数。如果区域性信息与转换无关,那么在自定义转换器中可以忽略该参数。
一、单值转换实例,IValueConverter
1.当值从绑定源传播给绑定目标时,调用方法Convert
2.当值从绑定目标传播给绑定源时,调用此方法ConvertBack,方法ConvertBack的实现必须是方法Convert的反向实现。
/// <summary> /// 自定义事件转换 /// </summary> public class TimeConver : IValueConverter { //当值从绑定源传播给绑定目标时,调用方法Convert public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return DependencyProperty.UnsetValue; DateTime date = (DateTime)value; return date.ToString("yyyy-MM-dd"); } //当值从绑定目标传播给绑定源时,调用此方法ConvertBack public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { string str = value as string; DateTime txtDate; if (DateTime.TryParse(str, out txtDate)) { return txtDate; } return DependencyProperty.UnsetValue; } }
注:返回值DependencyProperty.UnsetValue表示转换器没有生成任何值。
在xaml中引用TimeConver的命名空间
xmlns:local="clr-namespace:AudioDemo.View"
在xaml中定义Resources
<Window.Resources> <local:TimeConver x:Key="cvtDate"/></Window.Resources>
在xaml重指定Binding值使用自定义Converter转换
<TextBox x:Name="textBox" Text="{Binding ElementName=dateOne,Path=SelectedDate,Converter={StaticResource cvtDate}}" HorizontalAlignment="Left" Height="23" Margin="85,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="183"/>
Xaml文件内容:
<Grid> <DatePicker x:Name="dateOne" HorizontalAlignment="Left" Margin="85,50,0,0" VerticalAlignment="Top" Width="183" SelectedDateFormat="Long"/> <TextBox x:Name="textBox" Text="{Binding ElementName=dateOne,Path=SelectedDate,Converter={StaticResource cvtDate}}" HorizontalAlignment="Left" Height="23" Margin="85,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="183"/> <Label x:Name="label" Content="选择结果:" HorizontalAlignment="Left" Margin="19,105,0,0" VerticalAlignment="Top"/> <Label x:Name="label1" Content="{Binding ElementName=dateOne,Path=Text}" HorizontalAlignment="Left" Margin="85,145,0,0" VerticalAlignment="Top"/></Grid>
运行结果:
使用转换前: 使用自定义Converter转换后:
链接地址:http://www.cnblogs.com/tianma3798/p/5927470.html
- WPF Binding值转换器ValueConverter使用简介(一)
- WPF Binding值转换器ValueConverter
- wpf 的值转换器使用
- WPF中的Binding(一)
- 【WPF】binding的使用
- WPF 初步使用Binding
- 深入浅出WPF之Binding的使用(一)
- 【WPF】动态设置Binding的ConverterParameter转换器参数
- wpf值转换器用法
- WPF值转换器
- WPF 值转换器
- WPF 使用值转换器进行绑定数据的转换IValueConverter
- WPF之Binding基础一 UI Binding Source
- WPF中Binding的技巧(一)
- WPF中Binding的技巧(一)
- WPF的Binding学习笔记(一)
- WPF中Binding的技巧(一)
- WPF的Binding基础(一)
- 20170925_死锁的产生和消除
- 无法解析的外部符号问题小结
- angularjs 获取 ResponseEntity<String>
- Effective Java(数组和泛型的实现方式、用无限制的通配符提高API的灵活性)
- LeetCode:M-114. Flatten Binary Tree to Linked List
- WPF Binding值转换器ValueConverter使用简介(一)
- jitter in fMRI design
- C#学习笔记13-抽象类和接口
- MATLAB中cell数组的全面介绍
- U盘安装镜像WIN10 64 专业版 BMR转GPT 的问题
- Maven的安装
- Google支付服务端client_id和client_secret及refresh_token参数申请
- Hadoop完全分布式搭建过程
- [BZOJ1576]安全路径travel-最短路径树+并查集