WPF-Binding的源
1. 绑定到其它元素
<Grid>
<StackPanel>
<TextBox x:Name="textbox1" />
<Label Content="{Binding ElementName=textbox1, Path=Text}" />
</StackPanel>
</Grid>
2. 绑定到静态资源
<Window.Resources>
<ContentControl x:Key="text">Hello, World!</ContentControl>
</Window.Resources>
<Grid>
<StackPanel>
<Label x:Name="label1" Content="{Binding Source={StaticResource text}}" />
</StackPanel>
</Grid>
<STRONG>3. 绑定到自身</STRONG>
<Grid>
<StackPanel>
<Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource Self}, Path=Name}" />
</StackPanel>
</Grid>
4. 绑定到指定类型的父元素
1 <Grid x:Name="Grid1">
2 <StackPanel>
3 <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,
4 AncestorType={x:Type Grid}}, Path=Name}" />
5 </StackPanel>
6 </Grid>
5. 绑定到对象
1 public class Person
2 {
3 public string Name { get; set; }
4 public int Age { get; set; }
5 }
1 <StackPanel x:Name="stackPanel">
2 <StackPanel.DataContext>
3 <local:Person Name="Jack" Age="30"></local:Person>
4 </StackPanel.DataContext>
5 <TextBlock Text="{Binding Path=Name}"></TextBlock>
6 <TextBlock Text="{Binding Path=Age}"></TextBlock>
7
8 </StackPanel>
6. 绑定到集合
1 public class Person
2 {
3 public string Name { get; set; }
4 public int Age { get; set; }
5 }
6
7 public class PersonList : ObservableCollection<Person>
8 { }
01 <Window.Resources>
02 <local:PersonList x:Key="person">
03 <local:Person Name="Jack" Age="30"></local:Person>
04 <local:Person Name="Tom" Age="32"></local:Person>
05 </local:PersonList>
06 </Window.Resources>
07 <StackPanel x:Name="stackPanel">
08 <ListBox ItemsSource="{Binding Source={StaticResource ResourceKey=person}}"
09 DisplayMemberPath="Name">
10 </ListBox>
11 </StackPanel>
7. DataContext共享源
我们需要将同一资源绑定到多个 UI 元素上,很显然到处写 "{Binding Source={StaticResource person}}" 是件很繁琐且不利于修改的做法。WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。
01 <Window.Resources>
02 <local:PersonList x:Key="person">
03 <local:Person Name="Jack" Age="30"></local:Person>
04 <local:Person Name="Tom" Age="32"></local:Person>
05 </local:PersonList>
06 </Window.Resources>
07 <StackPanel x:Name="stackPanel" DataContext="{StaticResource person}">
08 <ListBox ItemsSource="{Binding}"
09 DisplayMemberPath="Name">
10 </ListBox>
11 </StackPanel>
8. 使用XML作为Binding的源
XML:
01 <?xml version="1.0" encoding="utf-8" ?>
02 <PersonList>
03 <Person Id="1">
04 <Name>Jack</Name>
05 </Person>
06 <Person Id="2">
07 <Name>Tom</Name>
08 </Person>
09 <Person Id="3">
10 <Name>Justin</Name>
11 </Person>
12 <Person Id="4">
13 <Name>David</Name>
14 </Person>
15 </PersonList>
XAML:
01 <StackPanel>
02 <ListView x:Name="personListView">
03 <ListView.View>
04 <GridView>
05 <GridViewColumn Header="Id" Width="100"
06 DisplayMemberBinding="{Binding XPath=@Id}"/>
07 <GridViewColumn Header="Name" Width="100"
08 DisplayMemberBinding="{Binding XPath=Name}"/>
09 </GridView>
10 </ListView.View>
11 </ListView>
12 <Button Click="Button_Click">Load Data</Button>
13 </StackPanel>
后台代码:
01 private void Button_Click(object sender, RoutedEventArgs e)
02 {
03 XmlDocument xmlDocument = new XmlDocument();
04 xmlDocument.Load("Person.xml");
05
06 XmlDataProvider xdp = new XmlDataProvider();
07 xdp.Document = xmlDocument;
08 xdp.XPath = @"/PersonList/Person";
09
10 this.personListView.DataContext = xdp;
11 this.personListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
12 }
WPF-Binding的源的更多相关文章
- WPF 基础 - Binding 的源与路径
1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...
- WPF Binding值转换器ValueConverter使用简介(一)
WPF.Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异 ...
- WPF Binding
winform有binding, WPF也有binding,区别在哪呢?这里暂时不提.以前也检查接触WPF binding, 但为什么过段时间就忘记了呢? 可能主要原因自己的知识体系不够完善吧,下面我 ...
- WPF Binding Mode,UpdateSourceTrigger
WPF 绑定模式(mode) 枚举值有5个1:OneWay(源变就更新目标属性)2:TwoWay(源变就更新目标并且目标变就更新源)3:OneTime(只根据源来设置目标,以后都不会变)4:OneWa ...
- 深入浅出-Binding的源与路径
1.把控件作为Binding源与Binding标记扩展<TextBox x:Name="textBox1" Text="{Binding Path=Value, E ...
- WPF Binding ElementName方式无效的解决方法--x:Reference绑定
原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...
- WPF binding 参考
Introduction This is an article on WPF Binding Cheat Sheet. Some of the Binding won't work for Silve ...
- Binding的源与路径
1.把控件作为Binding的源 例子:拖动Slider,输入框中的值也会跟着改变,或在输入框中输入数值,滑动条也会自动移动 <Window x:Class="把控件作为Binding ...
- WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProp ...
- WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter
注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于 ...
随机推荐
- cs-JsonHelper
ylbtech-Unitity: cs-JsonHelper AjaxResult.cs FormatJsonExtension.cs 1.A,效果图返回顶部 1.B,源代码返回顶部 1.B.1 ...
- win8 下脚本安装IIS
@echo off echo 正在添加IIS8.0 功能,依据不同的网络速率,全程大约需要5分钟时间... start /w pkgmgr /iu:IIS-WebServerRol ...
- 防止木马利用iframe框架来调用外域JS代码
<!--防止木马利用iframe框架来调用外域JS代码,不过滤自己网站的域名的框架网页开始--><SCRIPT LANGUAGE="JavaScript"> ...
- linux驱动摸索 --驱动框架初始化(结合韦东山视频教程)
一.驱动框架 初始化:insmod 加载 1.确定主设备号: 分为静态和动态分配,其中LED_GPIO_SIZE 表示支持的次设备号数目,一般默认为1. 相关实现代码如下: int result; d ...
- http://www.binghe.org/2010/03/use-httpsurlconnection-in-java/
http://www.binghe.org/2010/03/use-httpsurlconnection-in-java/
- 基于Spark机器学习和实时流计算的智能推荐系统
概要: 随着电子商务的高速发展和普及应用,个性化推荐的推荐系统已成为一个重要研究领域. 个性化推荐算法是推荐系统中最核心的技术,在很大程度上决定了电子商务推荐系统性能的优劣,决定着是否能够推荐用户真正 ...
- (转)spring boot实战(第六篇)加载application资源文件源码分析
原文:http://blog.csdn.net/liaokailin/article/details/48878447
- Agent是什么
广义的Agent包括人类.物理世界的机器人和信息世界的软件机器人. 狭义的Agent专指信息世界中的软件机器人或称软件Agent. 1) 弱定义 Agent用来最一般地说明一个软硬件系统,具有四个特性 ...
- 【转】线程同步------java synchronized详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...
- 在 XenServer上调试windows程序
WinDbg WinDbg is one of a number of tools available from Microsoft that can be used for debugging Wi ...