【WIN10】Bind、Binding與Converter的使用
Demo源碼下載:http://yunpan.cn/cHuCmI4NK4xwr 访问密码 8201
1.Bind
Bind的使用方式是:
- <Button Content="{x:Bind Path=xxxxx}" />
其中xxxx是Page所在的class的成員。如:
- class YourPage : Page {
- public string xxxx {get;set;}
- }
Path可以省略不寫。即:
- <Button Content={x:Bind xxxx} />
如果x:Bind使用在 ItemTemplate 中,則在 DateTemplate 屬性中必須指定 x:DateType。如:
- <Page.Resource>
- <DateTemplate
- x:Name="template1"
- x:DateType="local:ItemType">
- <Grid>
- <TextBlock Text="{x:Bind Title}" />
- </Grid>
- </DateTemplate>
- </Page.Resource>
其中local為xaml中所使用的名字空間替代者。而我們要聲明一個類:
- public class ItemType {
- public string Title {get;set;}
- }
如此,就可以將ItemTemplate與一個具體的類型綁定了。
2.Binding
Binding的使用方式最靈活。因為它可以指定使用某些控件變量。比如,我們有一個Slider和一個TextBox,Slider的值會隨著TextBox的值的改變而改變,或是TextBox的值隨著Slider的滑動而改變,或者誰的值改變,都會改變對方的值,這個時候,我們就必須要使用Binding了。如:
- <Grid>
- <TextBox
- x:Name="txtBox"
- Text="{Binding ElementName=slider, Path=Value, Mode=TwoWay}" />
- <Slider x:Name="slider" />
- </Grid>
ElemenetName指定了使用哪個控件,Path指定使用該控件的哪個屬性,Mode指定了綁定了方式。上面的這段代碼的意思主是:
- txtBox.Text = slider.Value.ToString();
- slider.Value = int.Parse(txtBox.Text);
因為Mode=TwoWay,表示是雙向綁定,即我的就是你的,你的就是我的。
Mode總共有三個屬性:
1.OneTime只綁定一次
2.OneWay單向綁定,即被綁定的對象值改變時,我就改變,而我改變時不需要去改變它。
3.TwoWay雙向綁定,我改變你也改變,你改變我也改變。
如果在ItemTemplate中使用Binding,直接使用類的成員變量即可。如:
- <Page.Resource>
- <DateTemplate
- x:Name="template1"
- >
- <Grid>
- <TextBlock Text="{Binding Title}" />
- </Grid>
- </DateTemplate>
- </Page.Resource>
注意與x:Bind的比較,DateTemplate 是不需要 x:DataType 屬性的。
3.Converter
顧名思義,Converter就是轉換器。它的作用是將控件的某的屬性的值,根據用戶的某些需求而轉換成用戶需要的值。
打個比方說,大陸的一款軟件,上面全是簡化字,當台灣、香港、澳門或是海外華人使用時,會很彆扭,或是認不出來是什麼字,這個時候我們需要一個轉換器,將簡化字轉換成繁體字,還可以根據地區轉換不同的繁體漢字。
又比方說,一個任務軟件,它有任務程度:允許暫緩、標準、緊急。我們的Grid的背景顏色,可以根據這些不同的程度,進行加亮、置暗或使用高亮顏色進行提醒用戶。
這些都可以使用轉換器進行解決。
我寫的一個例子如下:
XAML:
- <Page>
- <Page.Resources>
- <local:X2YConverter x:Name="x2yConverter" />
- </Page.Resources>
- <Grid>
- <TextBox Text="{Binding Converter={StaticResource x2yConverter}" />
- </Grid>
- </Page>
要在Resources中聲明一個X2YConverter對象。
聲明類X2YConverter:
- class X2YConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, string language)
- {
- string trueValue = value as string;
- string newValue = trueValue.ToLower();
- newValue = newValue.Replace("", "一");
- newValue = newValue.Replace("", "二");
- newValue = newValue.Replace("", "三");
- newValue = newValue.Replace("", "四");
- newValue = newValue.Replace("", "五");
- newValue = newValue.Replace("", "六");
- newValue = newValue.Replace("", "七");
- newValue = newValue.Replace("", "八");
- newValue = newValue.Replace("", "九");
- newValue = newValue.Replace("", "零");
- return newValue;
- }
- public object ConvertBack(object value, Type targetType, object parameter, string language)
- {
- throw new NotImplementedException();
- }
- }
上面這個類簡單地實現了把阿拉伯數字轉換成漢字零到九。
仔細觀看Convert函數,發現它還有 parameter 和 language 函數,這兩個函數均在Binding中指定。如:
- <TextBlock Text="{Binding Converter={StaticResource x2yConverter}, ConverterParameter=hehe, ConverterLanguage=chinese" />
最後,還看到一個沒有實現的 ConvertBack 。這個函數只有當綁定模式為 TwoWay 時才有用。因為當它綁定到另一個控件時,這兩個控件的值是你轉換成我,我轉換成你,一個用於你轉換成我,另一個用於我轉換成你。這種情況最適合用在值不同類型的綁定上。如:將一個Color為0xffff0000轉換成字符串“紅色”時,返回類型一個是 Color,另一個是 string。這個是需要注意的。具體的實例可以參見我上一片博客中,關於播放電影中Position與Slider的值綁定。
4.附加.ItemTemplateSelector
這個用於根據某些屬性來改變Item的DataTemplate。比如換膚就可以用這個玩意。
XAML:
- <Page>
- <Page.Resource>
- <local:ListItemTemplateSelector x:Name="listItemSelector"
- T1="{StaticResource listtemplateOption1}"
- T2="{StaticResource listtemplateOption2}"/>
- <DataTemplate x:Name="listtemplateOption1" x:DataType="local:MyListItem">
- <Grid Width="300">
- <TextBlock Text="{x:Bind Title}" />
- <Button Content="selector1" HorizontalAlignment="Right" />
- </Grid>
- </DataTemplate>
- <DataTemplate x:Name="listtemplateOption2">
- <Grid Width="300">
- <TextBlock Text="{Binding}" Foreground="Red"/>
- <Button Content="selector2" HorizontalAlignment="Right" />
- </Grid>
- </DataTemplate>
- </Page.Resources>
- </Page.Resource>
- <Grid>
- <ListView
- ItemsSource="{x:Bind MultiItems}"
- ItemTemplateSelector="{StaticResource listItemSelector}"
- />
- </Grid>
- </Page>
定義MyListItem和DataTemplateSelector:
- public class MyListItem
- {
- public string Title { get; set; }
- }
- public class ListItemTemplateSelector : DataTemplateSelector {
- public DataTemplate T1 { get; set; }
- public DataTemplate T2 { get; set; }
- protected override DataTemplate SelectTemplateCore(System.Object item)
- {
- if (item.GetType() == typeof(MyListItem))
- {
- return T1;
- }
- return T2;
- }
- }
然後弄個List,裡面包括string 和 MyListItem 兩種類型,可以根據不同的類型使用不同的模板進行顯示。截圖如下:
【WIN10】Bind、Binding與Converter的使用的更多相关文章
- [WPF]Binding的Converter和Validator
不拘一格用数据的Converter 上篇文已经说明,Binding就是数据源与目标之间的“关联”.大多数情况下,数据从Source到Target以及从Target返回Source都是“直来直去”的,但 ...
- WPF的DataGrid的某个列绑定数据的三种方法(Binding、Converter、DataTrigger)
最近在使用WPF的时候,遇到某个列的值需要根据内容不同进行转换显示的需求.尝试了一下,大概有三种方式可以实现: 1.传统的Binding方法,后台构造好数据,绑定就行. 2.转换器方法(Convert ...
- Chisel3 - bind - Binding
https://mp.weixin.qq.com/s/2318e6VJ4wFGpWwBOmTikA Chisel数据类型(Data)与Module的绑定关系,根据Data的使用方式不同,而有多种绑 ...
- C# wpf中关于binding的converter无效的情况
最近碰到bingding设置了convert转换无效的问题.困扰了我好久.这里记录分析一下. 先说下现象 我把TextBox的text属性 绑定到了对应的 convert.代码如下 希望吧pd_no ...
- UWP开发之Mvvmlight实践四:{x:bind}和{Binding}区别详解
{x:bind}是随着UWP被推出而被添加的,可以说是Win10 UWP开发专有扩展.虽然 {x:Bind} 缺少{Binding} 中的一些功能,但它运行时所花费的时间和使用的内存量均比 {Bind ...
- 电量显示Binding Converter MVVM
用一个ProcessBar显示电量,低于20%时候,ForeGround为红色,否则为绿色, 页面使用了MVVM绑定到了ViewModel, ProcessBar XAML为 <Progress ...
- How to bind to data when the DataContext is not inherited【项目】
http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherit ...
- UWP: 掌握编译型绑定 x:Bind
在 UWP 开发中,我们在进行数据绑定时,除了可以使用传统的绑定 Binding,也可以使用全新的 x:Bind,由于后者是在程序编译时进行初始化操作(不同于 Binding,它是在运行时创建.初始化 ...
- win10 uwp 商业游戏 1.2.1
上一个游戏已经告诉大家如何写多个游戏,现在继续写这个无聊的游戏 希望大家在看这篇文章之前先看win10 uwp 商业游戏,在这个文章告诉了大家如何创建游戏. 修改数值 可以从上一篇的博客的游戏看到升级 ...
随机推荐
- Crash Consistency : FSCK and Journaling
现在开始今天的第三篇博客的撰写,不能扯淡了,好多任务啊.但是还是忍不住吐槽一下,之前选择这篇文章纯属是个意外,我把Crash看做了Cache,唉,要不然也就不用写这篇文章了. 1. 这篇博客讲什么? ...
- 【CodeForces】947 C. Perfect Security 异或Trie
[题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...
- 天梯赛 L2-012 关于堆的判断 (二叉树)
将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: "x is the root":x是根结点: "x and y ar ...
- python mysql参数化查询防sql注入
一.写法 cursor.execute('insert into user (name,password) value (?,?)',(name,password)) 或者 cursor.execut ...
- Axure RP 授权码
Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...
- 不老的神器:安全扫描器Nmap渗透使用指南【转】
介绍 nmap是用来探测计算机网络上的主机和服务的一种安全扫描器.为了绘制网络拓扑图Nmap的发送特制的数据包到目标主机然后对返回数据包进行分析.Nmap是一款枚举和测试网络的强大工具. 特点 主机探 ...
- 20180830 安装git时报错,
安装:https://blog.csdn.net/u013256816/article/details/54743470 解决问题:https://blog.csdn.net/daojibruce/a ...
- golang基础之一
一.第一个go程序 package main import ( "fmt" ) func main(){ fmt.Println("hello world") ...
- 高屋建瓴之WebMail攻与防
0x01:前言 随着互联网的快速发展,我们的生活与互联网的联系愈加的紧密.各种快捷方便的信息化通信工具渐渐取代了传统的通信方式.微博.QQ.MSN.微信.陌陌, …这样的社交软件和平台已经成为了我们生 ...
- CSRF攻击的应对之道
CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执 ...