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的使用的更多相关文章

  1. [WPF]Binding的Converter和Validator

    不拘一格用数据的Converter 上篇文已经说明,Binding就是数据源与目标之间的“关联”.大多数情况下,数据从Source到Target以及从Target返回Source都是“直来直去”的,但 ...

  2. WPF的DataGrid的某个列绑定数据的三种方法(Binding、Converter、DataTrigger)

    最近在使用WPF的时候,遇到某个列的值需要根据内容不同进行转换显示的需求.尝试了一下,大概有三种方式可以实现: 1.传统的Binding方法,后台构造好数据,绑定就行. 2.转换器方法(Convert ...

  3. Chisel3 - bind - Binding

    https://mp.weixin.qq.com/s/2318e6VJ4wFGpWwBOmTikA   Chisel数据类型(Data)与Module的绑定关系,根据Data的使用方式不同,而有多种绑 ...

  4. C# wpf中关于binding的converter无效的情况

    最近碰到bingding设置了convert转换无效的问题.困扰了我好久.这里记录分析一下. 先说下现象 我把TextBox的text属性  绑定到了对应的 convert.代码如下 希望吧pd_no ...

  5. UWP开发之Mvvmlight实践四:{x:bind}和{Binding}区别详解

    {x:bind}是随着UWP被推出而被添加的,可以说是Win10 UWP开发专有扩展.虽然 {x:Bind} 缺少{Binding} 中的一些功能,但它运行时所花费的时间和使用的内存量均比 {Bind ...

  6. 电量显示Binding Converter MVVM

    用一个ProcessBar显示电量,低于20%时候,ForeGround为红色,否则为绿色, 页面使用了MVVM绑定到了ViewModel, ProcessBar XAML为 <Progress ...

  7. 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 ...

  8. UWP: 掌握编译型绑定 x:Bind

    在 UWP 开发中,我们在进行数据绑定时,除了可以使用传统的绑定 Binding,也可以使用全新的 x:Bind,由于后者是在程序编译时进行初始化操作(不同于 Binding,它是在运行时创建.初始化 ...

  9. win10 uwp 商业游戏 1.2.1

    上一个游戏已经告诉大家如何写多个游戏,现在继续写这个无聊的游戏 希望大家在看这篇文章之前先看win10 uwp 商业游戏,在这个文章告诉了大家如何创建游戏. 修改数值 可以从上一篇的博客的游戏看到升级 ...

随机推荐

  1. Spark 基本架构及原理

    转载自: http://blog.csdn.net/swing2008/article/details/60869183 转自:http://www.cnblogs.com/tgzhu/p/58183 ...

  2. windows下gitlab配置 生成ssh key

    Git-1.9.5-preview20141217 1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C "e ...

  3. 面向对象 ( OO ) 的程序设计——理解对象

    本文地址:http://www.cnblogs.com/veinyin/p/7607938.html  1 创建自定义对象 创建自定义对象的最简单方法为创建 Object 的实例,并添加属性方法,也可 ...

  4. Go语言 8 反射

    文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 8.1概念和作用 Reflection(反射)在计算 ...

  5. EasyUi组合条件分页查询

    1.引入css与js文件 <link rel="stylesheet" type="text/css" href="themes/default ...

  6. python使用unittest模块selenium访问斗鱼获取直播信息

    import unittest from selenium import webdriver from bs4 import BeautifulSoup as bs class douyu(unitt ...

  7. ThoughtWorks代码挑战——FizzBuzzWhizz游戏 通用高速版(C/C++ & C#)

    最早看到这个题目是从@ 程序媛想事儿(Alexia) 的 最难面试的IT公司之ThoughtWorks代码挑战——FizzBuzzWhizz游戏 开始的,然后这几天陆陆续续有N个小伙伴发表了自己的文章 ...

  8. Dagger:快速的依赖注入for 安卓&Java

    Dagger:快速的依赖注入for 安卓&Java 2014年5月8日 星期四 15:29 官网: http://square.github.io/dagger/ GitHub: https: ...

  9. 浅谈BeanUtils的拷贝,深度克隆

    1.BeanUtil本地简单测试在项目中由于需要对某些对象进行深度拷贝然后进行持久化操作,想到了apache和spring都提供了BeanUtils的深度拷贝工具包,自己写了几个Demo做测试,定义了 ...

  10. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...