数据绑定(十)Binding的数据转换
当Source端Path所关联的数据与Target端目标属性数据类型不一致时,需要添加数据转换器,数据转换器是一个自定义的类,这个类需要实现IValueConverter接口,这个接口有两个方法需要实现:Convert和ConvertBack,当数据从Source流向Target时,将调用Convert方法,反之,将调用ConvertBack方法
例子,首先定义飞机类型
public enum Category
{
Bomber,
Fighter
} public enum State
{
Available,
Locked,
Unknown
} public class Plane
{
public Category Category { get; set; }
public string Name { get; set; }
public State State { get; set; }
}
Plane类型的Category将在界面上转换为图片,而State类型将会转换成界面上的CheckBox显示,由于存在两个转换,因此需要提供两个Converter,第一个转换是做Category类型与字符类型的转换,字符串是图片的路径
class CategoryToSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Category c = (Category)value;
switch (c)
{
case Category.Bomber:
{
return @"\Icons\close.png";
}
case Category.Fighter:
{
return @"\Icons\closeing.png";
}
default:
{
return null;
}
}
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new NotImplementedException();
}
}
由于UI上不能修改图片,所以只实现了从Source到Target的转换
另一个转换用于将State数据转换为bool?
public class StateToNullableBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
State s = (State)value;
switch (s)
{
case State.Locked:
{
return false;
}
case State.Available:
{
return true;
}
case State.Unknown:
default:
{
return null;
}
}
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool? nb = (bool?)value;
switch (nb)
{
case true:
{
return State.Available;
}
case false:
{
return State.Locked;
}
case null:
default:
{
return State.Unknown;
}
}
}
}
界面代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="275" Width="275">
<Window.Resources>
<local:CategoryToSourceConverter x:Key="cts" />
<local:StateToNullableBoolConverter x:Key="stnb" />
</Window.Resources>
<StackPanel Background="LightBlue">
<ListBox x:Name="listBoxPlane" 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="buttonSave" Content="Save" Height="25" Margin="5,0" Click="buttonSave_Click" />
</StackPanel>
</Window>
加粗的部分是XAML中对转换器的使用,后台代码中实现了Load和Save两个按钮的点击事件
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.Bomber, Name="B-2", State=State.Unknown},
new Plane(){Category=Category.Fighter, Name="F-22", State=State.Unknown},
new Plane(){Category=Category.Fighter, Name="Su-47", State=State.Unknown},
new Plane(){Category=Category.Bomber, Name="B-52", State=State.Unknown},
new Plane(){Category=Category.Fighter, Name="J-10", State=State.Unknown}
}; listBoxPlane.ItemsSource = planeList;
} private void buttonSave_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (Plane p in listBoxPlane.Items)
{
sb.AppendLine(string.Format("Category={0}, Name={1}, State={2}", p.Category, p.Name, p.State));
}
File.WriteAllText(@"d:\PlaneList.txt", sb.ToString());
}
界面效果如图:
当改变checkbox的选中状态时,Plane对象中的值会发生变化
数据绑定(十)Binding的数据转换的更多相关文章
- Kendo MVVM 数据绑定(十) Source
Kendo MVVM 数据绑定(十) Source Source 绑定可以把 ViewModel 的值和由 Kendo 模板定义的目标元素绑定,如果 ViewModel 的值发生变化,被绑定的目标元素 ...
- WPF QuickStart系列之数据绑定(Data Binding)
这篇博客将展示WPF DataBinding的内容. 首先看一下WPF Data Binding的概览, Binding Source可以是任意的CLR对象,或者XML文件等,Binding Targ ...
- Windows phone 8.1之数据绑定(Data Binding)
学习Winphone8.1的时候经常需要对Slider进行数据绑定后使之视觉化,方便调节Slider的值. 数据绑定分为源(Source)和目标(Target),Source一般分为两种,其他控件的数 ...
- AngularJS基础02 神奇的数据绑定(Binding)
作者:arccosxy 转载请注明出处:http://www.cnblogs.com/arccosxy/ 上一节,我们在JS中声明一个scope变量然后在HTML直接访问它,这非常的酷.但是Angu ...
- WPF中的数据绑定Data Binding使用小结
完整的数据绑定的语法说明可以在这里查看: http://www.nbdtech.com/Free/WpfBinding.pdf MSDN资料: Data Binding: Part 1 http:// ...
- WPF学习09:数据绑定之 Binding to List Data
从WPF学习03:Element Binding我们可以实现控件属性与控件属性的绑定. 从WPF学习07:MVVM 预备知识之数据绑定 我们可以实现控件属性与自定义对象属性的绑定. 而以上两个功能在实 ...
- [WPF系列]-DataBinding(数据绑定) 自定义Binding
自定义Binding A base class for custom WPF binding markup extensions BindingDecoratorBase Code: public c ...
- WPF之数据绑定Data Binding
一般情况下,应用程序会有三层结构:数据存储层,数据处理层(业务逻辑层),数据展示层(UI界面). WPF是“数据驱动UI”. Binding实现(通过纯C#代码) Binding分为source和ta ...
- Kendo UI开发教程(21): Kendo MVVM 数据绑定(十) Source
Source绑定可以把ViewModel的值和由Kendo模板定义的目标元素绑定,如果ViewModel的值发生变化,被绑定的目标元素也随之发生变化.模板由属性data-template指定,它的值为 ...
随机推荐
- Android中的动画详解系列【3】——自定义动画研究
在上一篇中我们使用到了位移动画TranslateAnimation,下面我们先来看看TranslateAnimation是如何实现Animation中的抽象方法的: /* * Copyright (C ...
- php正则及常用正则函数怎么用
php正则及常用正则函数怎么用 一.总结 一句话总结: 能够使用正则的函数:preg_match();preg_match_all();preg_replace();preg_grep();preg_ ...
- Expression Blend 的点滴(1)--ListBox华丽大变身
原文:Expression Blend 的点滴(1)--ListBox华丽大变身 最近,在园子里有不少朋友写了关于Blend的优秀并且实用的文章,在此,我先代表silverlight的爱好者感谢他们的 ...
- C#基础readonly 与const
readonly 与 const readonly是运行时常量,const是编译期常量(在编译过程中已经把使用该值的都用值替代,不分配内存)readonly灵活性高,const效率高 readonly ...
- 【BZOJ 1016】 [JSOI2008]最小生成树计数(matrix-tree定理做法)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1016 [题意] [题解] /* 接上一篇文章; 这里用matrix-tree定理搞最小 ...
- 常见排序算法(java实现)
常见排序算法介绍 冒泡排序 代码: public class BubbleSort { public static void sort(int[] array) { int tValue; for ( ...
- bootstrap 模态框 video视频测试
链接:https://files.cnblogs.com/files/tele-share/%E8%A7%86%E9%A2%91%E6%B5%8B%E8%AF%95.7z <!DOCTYPE h ...
- Node.js,一生所爱
下午参加了<云品秀--前端前沿>,用友云平台前端架构师郭永峰(站着的那位)讲得很棒,而我最关注的就是Node了.最后我问了他关于独立开发,后端选择Node还是别的语言.他讲了很多,说自己在 ...
- DapperPoco
DapperPoco -- 基于Dapper的.轻量级的.高性能的.简单的.灵活的ORM框架 为什么要重复造轮子 因为现有的轮子都在某些方面不太令我满意,下面我来一一点评一下,欢迎拍砖. Entity ...
- C#中的并发编程知识
= 导航 顶部 线程 Task async/await IAsyncResult Parallel 异步的回调 顶部 线程 Task async/await IAsyncResult Pa ...