【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 商业游戏,在这个文章告诉了大家如何创建游戏. 修改数值 可以从上一篇的博客的游戏看到升级 ...
随机推荐
- MagicB.0—怎样设置电脑自动关机?
天太晚了,该睡觉了,可是你的东西也许正在下载,软件正在更新,总之电脑还有一些工作没有完成,又不需要你人为的守着,随他去吧!可是电脑已经工作了一天了,它也要休息一下,再者也不能浪费电力资源呀,那么就来使 ...
- 51nod 1161 Partial Sums
给出一个数组A,经过一次处理,生成一个数组S,数组S中的每个值相当于数组A的累加,比如:A = {1 3 5 6} => S = {1 4 9 15}.如果对生成的数组S再进行一次累加操作,{1 ...
- Windows.event
获取鼠标位置 document.getElementById('div').onmousemove = function(e){ //--------Chrome, firefox 支持------- ...
- 多线程中的超时, 如Socket超时
; ,,, ->$port { print "-->$port\r"; #say "\r"; await Promise.anyof( Promis ...
- Thinkphp的CURD
CURD即(Create Update Read Delete)其实也就是等同于增删改查. C:Create 创建数据 对数据的添加 Create$m=new Model('User');$m=M( ...
- keepalived主备切换后的arp问题【转】
使用keepalived的时候主机挂了,备机显示绑定了VIP.但是此时实际还是不能访问.其实就是网关的arp缓存没有刷新. 在服务器上执行一下就行了 arping -I eth0 -c 5 -s ...
- MemCached缓存操作
Web项目在运行时,通常需要从数据库中进行读写.随着操作数据量的增大,以及访问量的集中,数据库的负载增加,数据库响应变慢,网站访问速度变慢的情况.Memcached就是用来解决这些问题的. Memca ...
- Bootstrap的使用。。。
概览 深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML5 文档类型 Bootstrap 使用到的某些 HTML 元素和 CSS 属 ...
- LeetCode312. Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- hdu 5001(概率DP)
Walk Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...