【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 商业游戏,在这个文章告诉了大家如何创建游戏. 修改数值 可以从上一篇的博客的游戏看到升级 ...
随机推荐
- selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉菜单事件的处理
使用Selenium爬虫时,可能会遇到一些下拉菜单,动态加载,如果直接使用find_element_by_函数会报错,显示selenium.common.exceptions.ElementNotVi ...
- asp.net mvc 站点优化
基于上篇:IIS网站日志分析 现象 服务端:IIS 日志, #Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-u ...
- 关于NuGet
一.NuGet是什么? NuGet是Microsoft开发平台的程序集包管理器,它由客户端工具和服务端站点组成,客户端工具提供给用户管理和安装/卸载软件程序包,以及打包和发布程序包到NuGet服务端站 ...
- form表单有条件的提交
form表单提交数据,比如,积分,score,在0--100之间 var score = $('#score').val(); if(score !=''){ if(isNaN(score)){ la ...
- 在Java中,你真的会日期转换吗
1.什么是SimpleDateFormat 在java doc对SimpleDateFormat的解释如下: SimpleDateFormat is a concrete class for form ...
- JWT机制了解
JWT简介 JSON Web Token(JWT)是一个开放式标准(RFC 7519),它定义了一种紧凑(Compact)且自包含(Self-contained)的方式,用于在各方之间以JSON对象安 ...
- Axure RP 授权码
Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...
- scandir函数的研究【笔记】
以下是本人的学习笔记,代码并非原创,均摘自官方源码,贴出来仅供学习记录用 scandir 的使用要注意内存泄漏的问题 scandir函数实现: vi ./uClibc-0.9.33.2/libc/mi ...
- maven的初步理解
[情景] 在进行JAVA项目开发的过程中,代码写好后,需要经过编译.打包.运行.测试.部署等过程. 在JAVA项目的开发阶段,就会根据业务的需要引入许多jar包来实现功能,但我们需求的jar包本身可能 ...
- charles抓包误点deny处理办法及日常抓包
误点deny方法在最底下~~ (博文为转载) 我们在开发网站项目的时候,我们可以通过浏览器的debug模式来看request以及response的数据,那么如果我们开发移动端项目没有网页呢?如何抓取数 ...