Binding的数据转换(Data Converter)
2015-10-16 14:19
429 查看
当Source端Path所关联的数据与Target端目标属性数据类型不一致时,添加数据转换器(Data Converter), 简单的数据转换(如double转string)WPF类库会自动转换。 手动转换时需要手动写Converter,创建一个类,并实现IValueConverter接口。
public interface IValueConverter { object Convert(object value, Type targetType, object parameter, CultureInfo culture);//当数据从Binding的Source流向Target时调用 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);//反之调用ConvertBack }
例:
用于在UI上显示图片和将Source的三个值映射到CheckBox控件的IsChecked属性值(bool?类型)
//种类,用于显示图片 public enum Category { Bomber, Fighter } //状态 CheckBox对应的三种状态 public enum State { Available, Locked, Unknown } //飞机类 public class Plane { public Category Category { set; get; } public string Name { set; get; } public State State { set; get; } }
提供两个Converter用于转换数据,一个将Category单项转为string(XAML自动解析为图片),另一个实现State与bool?的双向转换。
public class CategoryToSourceConverter :IValueConverter { //将Category转换为Uri public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Category c = (Category)value; switch (c) { case Category.Bomber: return @"\Icons\Bomber.png";//数据在项目Icons目录里 case Category.Fighter: return @"\Icons\Fighter.png"; default: return null; } } //不被调用 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } //将Stae转换为bool? public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { State s = (State)value; switch (s) { case State.Available: return true; case State.Locked: return false; case State.Unknown: default: return null; } } //将bool?转换为State public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool? nb = (bool?)value; switch (nb) { case true: return State.Available; case false: return State.Locked; case null: default: return State.Unknown; } }
之后再XAML里消费这些Converter
<Window x:Class="Windows6.Window6_4_2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Windows6.BLL" Title="Window6_4_2" Height="266" Width="300"> <Window.Resources> <local:CategoryToSourceConverter x:Key="cts"/> <local:StateToNullableBollConverter x:Key="stnb"/> </Window.Resources> <StackPanel Background="LightBlue"> <ListBox x:Name="listBoxPlan" Height="160" Margin="5"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Width="20" Height="20" Source="{Binding Path=Category, Converter={StaticResource cts}}"/> <TextBlock Text="{Binding Path=Name}" Width="60" Margin="80,0"/> <CheckBox IsThreeState="True" IsChecked="{Binding Path=State, Converter={StaticResource stnb}}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Button x:Name="buttonLoad" Content="Load" Height="25" Margin="5,0" Click="buttonLoad_Click"/> <Button x:Name="btnSave" Content="Save" Height="25" Margin="5,5" Click="btnSave_Click"/> </StackPanel> </Window>
最后实现两个按钮的点击事件
private void buttonLoad_Click(object sender, RoutedEventArgs e) { List<Plane> planeList = new List<Plane>() { new Plane() {Category = Category.Bomber, Name = "B-1", State = State.Unknown}, new Plane() {Category = Category.Fighter, Name = "F-22", State = State.Unknown} }; this.listBoxPlan.ItemsSource = planeList; } private void btnSave_Click(object sender, RoutedEventArgs e) { StringBuilder sb = new StringBuilder(); foreach (Plane p in listBoxPlan.Items) { sb.AppendLine(string.Format("Category={0},Name={1},State={2}", p.Category, p.Name, p.State)); } File.WriteAllText(@"D:\test\PlaneList.txt", sb.ToString()); }
相关文章推荐
- javac -encoding utf8 HelloWorld.java ,错误:非法字符:\65279
- iOS RGB 颜色对照表
- Reveal UI调试利器,逆向app的实践教程:Reveal别人的 app(一)
- android AsyncTask介绍
- Ansi,UTF8,Unicode,ASCII编码的区别
- 在linux下执行一个简单的C程序
- iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
- Java was started but returned exit code=13
- Maven中央仓库查询
- 收藏-如何配置一个高效的 Mac 工作环境
- WPF如何获得变量异步回调函数时产生的异步回调
- Android内存优化(使用SparseArray和ArrayMap代替HashMap)
- JS获取request字符串
- vs/vc++中无法解析的外部符号
- Linux多线程基础学习(四)线程的终止、连接、取消
- ps简单去除图片上的文字水印方法介绍
- 自定义渲染器
- java拆装箱(转)
- Java基础知识学习(五)
- 线程间通信的实现 wait()和notify()方法