首页 技术 正文
技术 2022年11月18日
0 收藏 788 点赞 4,588 浏览 5855 个字

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;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918