Windows Store apps开发[53]数据绑定(2)转换器
2012-11-17 00:21
363 查看
注:本文由BeyondVincent(破船)原创首发
转载请注明出处:BeyondVincent(破船)@DevDiv.com
更多内容请查看下面的帖子
[DevDiv原创]Windows
8 开发Step by Step
小引
在数据绑定中,有时候我们要显示的内容跟绑定的源有所不同:比如:考生的分数60分以下为不及格或者糟糕、60-90为及格或还行,90分以上为优秀或非常棒。那么我们就需要对绑定源的数据进行处理。这篇文章,我就来说一说绑定中的转换器——通过转换器可以达到对数据的处理,以显示想要的内容。简介
由于上一篇文章中我只是介绍了绑定模型,没有对数据绑定进行介绍,那么我们先来看看下面这个图(来自msdn),以便对数据绑定有一个了解:上面这个图描述了数据绑定的如下内容:
A、绑定目标——必须是依赖属性,一般来说,都是在FrameworkElement中。
B、绑定对象——连接绑定源与绑定目标的一个对象,绑定引擎通过绑定对象,对绑定源和绑定目标进行相应的处理
C、绑定源——也就是数据的来源,只要是CLR对象即可,不一定必须是依赖属性
E、转换器——对绑定源与绑定目标的值进行转换的接口
本文就是使用到上面的转换器。
使用转换器的关键技术
A、实现IValueConverter接口B、创建一个转换器,告诉绑定对象使用该转换器
实现步骤
A、新建空白工程,名称为BV_Binding_ConvertB、在项目工程上单击右键,选择添加->选择类->输入DataConverter,然后确定
C、在DataConverter.cs文件实现IValueConverter接口
IValueConverter接口需要重写两个函数:
Convert——当数据从数据源传递过来了,绑定引擎会掉用该方法,并将数据返回给绑定目标
ConvertBack——与Convert相反:当数据从绑定目标传递过来了,绑定引擎会掉用该方法,并将数据返回给绑定源
注意:在下面的代码中,我还利用了parameter参数,这是在绑定的时候可以指定的参数。可以在这里做一些判断。我在这里根据这个参数回返回不同的值。
另:在代码中我使用的是默认绑定模型:单向绑定,所以不会用到ConvertBack,我也就没有做具体的实现代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.UI.Xaml.Data; namespace BV_Binding_Convert { public class DataConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string type = parameter.ToString(); double count = (double)value; string temp; if (count < 60) { if (type == "1") temp = "不及格"; else temp = "糟糕"; } else if (count >= 60 && count <= 90) { if (type == "1") temp = "及格"; else temp = "还行"; } else { if (type == "1") temp = "优秀"; else temp = "非常棒"; } return temp; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } }
D、在xaml中使用绑定
我定义了一个Slider控件——当作绑定源
3个TextBlock当作绑定目标:1个是没有使用转换器,另外两个使用转换器,并携带了不同的参数。
具体代码如下:
<Page x:Class="BV_Binding_Convert.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:BV_Binding_Convert" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.Resources> <local:DataConverter x:Key="DataConverter1"/> </Page.Resources> <!--LayoutRoot 是包含所有页面内容的根网格--> <Grid x:Name="LayoutRoot" Background="Transparent"> <!--ContentPanel - 在此处放置其他内容--> <Grid Background="Green"> <Slider Name="slider" Foreground="Red" Value="60" Maximum="100" Margin="326,220,352,149"/> <TextBlock FontSize="50" Text="{Binding ElementName=slider, Path=Value}" Margin="554,286,542,254"/> <TextBlock FontSize="60" Text="{Binding ElementName=slider, Path=Value, Converter={StaticResource DataConverter1}, ConverterParameter='1'}" Margin="554,366,542,174"/> <TextBlock FontSize="60" Text="{Binding ElementName=slider, Path=Value, Converter={StaticResource DataConverter1}, ConverterParameter='2'}" Margin="554,451,614,89"/> </Grid> </Grid> </Page>
E、运行效果
用鼠标滑动Slider控件,则绑定目标会根据Slider不同的值进行改变更新。
相关文章推荐
- Windows Store apps开发[7]视图模型与数据绑定
- Windows Store apps开发[51]数据绑定(1)绑定模型
- Windows Store apps开发[54]数据绑定(3)通知属性改变
- Windows Store apps开发[65]Windows 8 开发31日-第08日-本地和漫游数据
- Windows Store apps开发[73]Windows 8 开发31日-第16日-上下文菜单
- Windows Store apps开发[20]添加应用设置(SettingsPane)
- Windows Store apps开发[74]Windows 8 开发31日-第17日-剪贴板
- Windows Store apps开发[58]Windows 8 开发31日-第01日-一个空的应用程序
- Windows Store apps开发[27]使用NetworkInformation来判断和监听网络状态
- Windows Store apps开发[18]Metro Revealed Building Windows 8 apps with XAML and C#中文翻译全部汇总
- Windows Store apps开发[13]应用程序生命周期
- Windows Store apps开发[38]Windows 8中的Semantic Zoom(语义缩放)(1)介绍
- Windows Store apps开发[39]Windows 8中的Semantic Zoom(语义缩放)(2)SemanticZoom控件的使用
- Windows Store apps开发[41]FlipView控件的介绍和使用
- Windows Store apps开发[44]AppBar总结
- Windows Store apps开发[45]修改ProgressBar的颜色
- Windows App开发之集合控件与数据绑定
- 【万里征程——Windows App开发】数据绑定——简单示例、更改通知、数据转换
- Windows Store apps开发[83]Windows 8 开发31日-第25日-加速度计
- Windows Store apps开发[14]Step by Step已矣