《深入浅出WPF》笔记二
1、消息驱动与事件驱动
事件 即封装过的消息
2、数据驱动
3、Binding
Source、Target、Path、INotifyPropertyChanged结构
this.textBoxName.SetBinding(TextBox.TextProperty,new Binding("Name){Source=stu=new Student()});
4、把控件作为Binding的Source
<Slider x:Name="slider1" Maximum="100" Minimun="0"/>
<TextBox x:Name="textBox1" Text="{Binding Value,ElementName=slider1}"/>
在C#中一般不必使用ElementName属性,直接将对象赋值给Binding的Source属性即可。
5、Binding的Mode(数据流向)
TwoWay、OneWay、OnTime、OneWayToSource、Default
6、Binding的UpdateSourceTrigger
PropertyChanged、LostFocus、Explicit、Default
7、Binding的其他属性:NotifyOnSourceUpdated和NotifyOnTargetUpdated,监听激发的事件可以找出有哪些数据或控件被更新了。
8、Binding的Path
<TextBox x:Name="textBox1"/>
<TextBox x:Name="textBox2" Text="{Binding Path=Text.Length,ElementName=textBox1,Mode=OneWay}"/>
<TextBox x:Name="textBox3" Text="{Binding Path=Text[3],ElementName=textBox1,Mode=OneWay}"/>
当使用一个集合或DataView作为Binding源时,如果想把默认元素作为Path使用,则使用如下语法:
List<string> stirngList=new List<string>(){"Tim","Tom","Blog"};
this.textBox1.SetBinding(TextBox.TextProperty,new Binding("/"){Source=stringList});
this.textBox2.SetBinding(TextBox.TextProperty,new Binding("/Length"){Source=stringList});
this.textBox3.SetBinding(TextBox.TextProperty,new Binding("/[3]"){Source=stringList});
如果集合元素的属性仍然是一个集合,想把子集中的元素作为Path,则可以使用多级斜线的语法(类似文件路径的那种,一级一级往下找)。
9、当Binding的Source本身只是数据(string、int等)时,Binding的Path设置为“.”(XAML中可以省略该设置。)
10、为Binding指定Source的几种方法
(1)普通的CLR类型单个对象
如果实现了INotifyPropertyChanged接口,则在set属性时激发PropertyChanged事件来通知Binding数据已更新。
(2)普通CLR集合类型的对象(数组,List<T>,ObservableCollection<T>)
一般把控件的ItemsSource属性使用Binding关联到一个集合对象上。
(3)ADO.NET数据对象(DataTable和DataView等)
<Grid>
<ListView x:Name="listViewStudents">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
DataTable dt=this.Load();
this.listViewStudents.ItemsSource=dt.DefaultView;
或者
DataTable dt=this.Load();
this.listViewStudents.DataContext=dt;
this.listViewStudents.SetBinding(ListView.ItemsSourceProperty,new Binding());
(4)XmlDataProvider把XML数据指定为Source
可以把树状结构的XML数据作为Source指定给级联控件(TreeView和Menu)的Binding。
使用XML数据作为Binding的Source时,我们使用XPath属性来指定数据的来源,而不是Path属性。
<Window.Resources>
<XmlDataProvider x:Key="xdp" XPath="FileSystem/Folder">
<x:Data>
<FileSystem xmlns="">
<Folder Name="Books">
<Folder Name="Cooks">
<Folder Name="Cook1"/>
<Folder Name="Cook2"/>
</Folder>
<Folder Name="Tools">
<Folder Name="Tool1"/>
<Folder Name="Tool2"/>
</Folder>
</Folder>
</FileSystem>
</x:Data>
</XmlDataProvider>
</Window.Resources>
<Grid>
<TreeView ItemsSource="{Binding Source={StaticResouce xdp}}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding XPath=Folder}">
<TextBlock Text="{Binding XPath=@Name}"/>
</HierarchicalDataTemplate >
</TreeView.ItemTemplate>
</TreeView>
</Grid>
(5)依赖对象(Dependency Object)
(6)把容器的DataContext指定为Source
建立一个Binding,只设置Path而不设置Source,该Binding就会沿着控件树一层层往外找,直到找到带有Path指定属性的对象。如果到了控件树根部仍然找不到,那么该Binding就得不到数据。
<StackPanel>
<StackPanel.DataContext>
<local:Student Id="1" Age="29" Name="Tim"/>
</StackPanel.DataContext>
<Grid>
<StackPanel>
<TextBox Text="{Binding Path=Id}"/>
<TextBox Text="{Binding Path=Name}"/>
<TextBox Text="{Binding Path=Age}"/>
</StackPanel>
</Grid>
</StackPanel>
实际上,DataContext是一个依赖属性,当没有为当前控件的依赖属性显式赋值时,控件就会把自己容器的属性值当做自己的属性值。也就是属性值沿着元素树向下传递了。
当多个控件Binding关注同一个对象时,可以使用DataContext。
将A窗体中的控件或控件的值作为窗体A的DataContext,可以将该控件暴露给其他的窗体。
(7)通过ElementName属性指定Source
(8)RelativeSource
<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"
(9)ObjectDataProvider
用于数据源的数据不是通过属性而是通过方法暴露给外界的时候。
ObjectDataProvider odp=new ObjectDataProvider();
odp.ObjectInstance=new Calculator();
odp.MethodName="Add";
odp.MethodParameters.Add("");
odp.MethodParameters.Add(""); Binding bindingToArg1=new Binding("MethodParameters[0]")
{
Source=odp;
BindsDirectlyToSource=true;
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
}; Binding bindingToArg2=new Binding("MethodParameters[1]")
{
Source=odp;
BindsDirectlyToSource=true;
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
}; Binding bindingToResult=new Binding(".")
{
Source=odp;
}; this.textBoxArg1.SetBinding(TextBox.TextProperty,bindingToArg1);
this.textBoxArg2.SetBinding(TextBox.TextProperty,bindingToArg2);
this.textBoxResult.SetBinding(TextBox.TextProperty,bindingToResult);
(10)LINQ检索得到的数据对象作为数据源
从一个填充好的List<Student>对象中检索出所有名字以T开头的学生。
this.listViewStudents.ItemsSource=from stu in stilts where su.Name.StartsWith("T") select stu;
如果数据存放在一个填充好的DataTable对象里:
DataTable dt=this.GetDataTable();
this.listViewStudents.ItemsSource=
from row in dt.Rows.Cast<DataRow>()
where Convert.ToString(row["Name"]).StartsWith("T")
select new Student()
{
Id=int.Parse(row["Id"].ToString()),
Name=row["Name"].ToString(),
Age=int.Parse(row["Age"].ToString())
};
如果数据放在XML文件里:
XDocument xdoc=XDocument.Load(@"D:\RawData.xml");
this.listViewStudents.ItemsSource=
from element in xdoc.Descendants("Student")
where element.Attribute("Name").Value.StartsWith("T")
select new Student()
{
Id=int.Parse(element.Attribute("Id").Value);
Name=element.Attribute("Name").Value;
Age=int.Parse(element.Attribute("Age").Value);
};
11、Binding数据验证——ValidationRules
Binding默认认为来自Source的数据总是正确地,若要校验来自Source的数据,需将校验条件的ValidatesOnTargetUpdated属性设置为true。
12、Binding的数据转换——IValueConverter接口
13、多路Binding
用于UI需要显示的信息由不止一个数据来源决定的时候。
比如,新用户注册的时候,用户名输入一致,邮箱输入一致时,注册按钮才可用。
MutiBinding mb=new MultiBinding(){Mode=BindingMode.OneWay};
mb.Bindings.Add(new Binding("Text"){Source=this.textBox1});
mb.Bindings.Add(new Binding("Text"){Source=this.textBox2});
mb.Bindings.Add(new Binding("Text"){Source=this.textBox3});
mb.Bindings.Add(new Binding("Text"){Source=this.textBox4});
mb.Converter=new LoginMultiBindingConverter();
this.button1.SetBinding(Button.IsEnabledProperty,mb);
Convert()方法的具体实现:
if(!Value.Cast<string>().Any(text=>string.IsNullOrEmpty(text))
&& values[0].ToString()==values[1].ToString()
&& values[1].ToString()==values[3].ToString())
{
return true;
}
else
return false;
《深入浅出WPF》笔记二的更多相关文章
- 简明python教程笔记
自然字符串 如果你想要指示某些不需要如转义符那样的特别处理的字符串,那么你需要指定一个自然字符串.自然字符串通过给字符串加上前缀r或R来指定. r"Newlines are indicate ...
- 《简明python教程》笔记一
读<简明Python教程>笔记: 本书的官方网站是www.byteofpython.info 安装就不说了,网上很多,这里就记录下我在安装时的问题,首先到python官网下载,选好安装路 ...
- 学习笔记《简明python教程》
学习笔记<简明python教程> 体会:言简意赅,很适合新手入门 2018年3月14日21:45:59 1.global 语句 在不使用 global 语句的情况下,不可能为一个定义于函数 ...
- 简明Python教程 ~ 随书笔记
本文是阅读<简明Python教程>所做的随书笔记,主要是记录一些自己不熟悉的用法,或者所看到的比较有意思的内容,本书英文版A Byte of Python, 中文译版 简明Python教程 ...
- 笔记|《简明Python教程》:编程小白的第一本python入门书
<简明Python教程>这本书是初级的Python入门教材,初级内容基本覆盖,对高级内容没有做深入纠结.适合刚接触Python的新手,行文比较简洁轻松,读起来也比较顺畅. 下面是我根据各个 ...
- 《简明Python教程》学习笔记
<简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Pa ...
- 简明Python教程自学笔记——命令行通讯录
[前言]学习Python已经有一段时间了,相关的书籍资料也下载了不少,但是没有一本完整的看完,也没有编出一个完整的程序.今天下午比较清闲就把<简明Python教程>看了一遍,然后根据书里面 ...
- python读书笔记-《简明python教程》上
1月15日 <简明python教程>上 基本结构: 基础概念+控制流+函数+模块+数据结构+面向对象+I/O+异常+标准库+其他 1.概念 1-0 退出python linux: ...
- (原+转)简明 Python 教程:总结
简明 Python 教程 说明:本文只是对<简明Python教程>的一个总结.请搜索该书查看真正的教程. 第3章 最初的步骤 1. Python是大小写敏感的. 2. 在#符号右面的内容 ...
- 【转】简明 Python 教程
原文网址:http://woodpecker.org.cn/abyteofpython_cn/chinese/ 简明 Python 教程Swaroop, C. H. 著沈洁元 译www.byteof ...
随机推荐
- 对iOS中Delegate的理解
首先 协议protocol 和委托delegate 是两个完全不同的概念 放在一起说 是因为我们总是在同一个头文件里看到它们: 首先解释一下什么是委托 :举个例子 ,我工作的时候给你打电话,让你帮我 ...
- hdu 2034 - 集合操作
题意:集合A,B,计算集合差A-B(求只在集合A内的数) 解法: 选用STL内的集合set 1.建立set 1: #include<set> 2: 3: set<int> ...
- 嵌入式Linux系统开发环境搭建
Linux kernel Complier: http://supportopensource.iteye.com/blog/680483 sudo make mrproper 净化解 ...
- Objective-C之字典
//字典:(关键字 值) //插入代码字太小 // NSArray *array = [NSArray array];//空数组 // NSDictionary *dict ...
- 自然语言14_Stemming words with NLTK
https://www.pythonprogramming.net/stemming-nltk-tutorial/?completed=/stop-words-nltk-tutorial/ # -*- ...
- Java数据结构——用链表实现栈
//================================================= // File Name : LinkStack_demo //---------------- ...
- Python 调用 user32.dll
import ctypes h = ctypes.windll.LoadLibrary("C:\\Windows\\System32\\user32.dll") h.Message ...
- Xcode卡顿解决方案
1.禁用indexing 在终端(terminal) 输入 defaults write com.apple.dt.XCode IDEIndexDisable 并重启Xcode. (不是大神勿用哈,附 ...
- 彻底解决Eclipse自动补全变量名及变量名后面追加类型名
彻底解决Eclipse自动补全变量名问题的方法步骤 发布于 2014-11-04 14:53 已被阅读 31613159 次 大家使用eclipse或者MyEclipse敲代码的时候,是不是都被这 ...
- A股博弈笔记
A股博弈笔记 @2014/11/07 A股行情最近甚嚣尘上,似乎是牛市的前奏,指数虽然见涨,但赔钱的股民估计也不少,本人就是其中一个,是我在逆势而行吗? 试图追逐价值投资的方式,而钟情于蓝筹股,本来也 ...