WPF绑定方式
绑定到其它元素
<Grid>
<StackPanel>
<TextBox x:Name="textbox1" />
<Label Content="{Binding ElementName=textbox1, Path=Text}" />
</StackPanel>
</Grid>
绑定到静态资源
<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>
绑定到指定类型的父元素
<Grid x:Name="Grid1">
<StackPanel>
<Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Grid}}, Path=Name}" />
</StackPanel>
</Grid>
绑定到对象
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
<StackPanel x:Name="stackPanel">
<StackPanel.DataContext>
<local:Person Name="Jack" Age="30"></local:Person>
</StackPanel.DataContext>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
<TextBlock Text="{Binding Path=Age}"></TextBlock>
</StackPanel>
绑定到集合
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonList : ObservableCollection<Person>
{ }
<Window.Resources>
<local:PersonList x:Key="person">
<local:Person Name="Jack" Age="30"></local:Person>
<local:Person Name="Tom" Age="32"></local:Person>
</local:PersonList>
</Window.Resources>
<StackPanel x:Name="stackPanel">
<ListBox ItemsSource="{Binding Source={StaticResource ResourceKey=person}}"
DisplayMemberPath="Name">
</ListBox>
</StackPanel>
DataContext共享源
我们需要将同一资源绑定到多个 UI 元素上,很显然到处写 "{Binding Source={StaticResource person}}" 是件很繁琐且不利于修改的做法。WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。
<Window.Resources>
<local:PersonList x:Key="person">
<local:Person Name="Jack" Age="30"></local:Person>
<local:Person Name="Tom" Age="32"></local:Person>
</local:PersonList>
</Window.Resources>
<StackPanel x:Name="stackPanel" DataContext="{StaticResource person}">
<ListBox ItemsSource="{Binding}"
DisplayMemberPath="Name">
</ListBox>
</StackPanel>
使用XML作为Binding的源
XML:
<?xml version="1.0" encoding="utf-8" ?>
<PersonList>
<Person Id="1">
<Name>Jack</Name>
</Person>
<Person Id="2">
<Name>Tom</Name>
</Person>
<Person Id="3">
<Name>Justin</Name>
</Person>
<Person Id="4">
<Name>David</Name>
</Person>
</PersonList>
XAML:
<StackPanel>
<ListView x:Name="personListView">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" Width="100"
DisplayMemberBinding="{Binding XPath=@Id}"/>
<GridViewColumn Header="Name" Width="100"
DisplayMemberBinding="{Binding XPath=Name}"/>
</GridView>
</ListView.View>
</ListView>
<Button Click="Button_Click">Load Data</Button>
</StackPanel>
private void Button_Click(object sender, RoutedEventArgs e)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("Person.xml");
XmlDataProvider xdp = new XmlDataProvider();
xdp.Document = xmlDocument;
xdp.XPath = @"/PersonList/Person";
this.personListView.DataContext = xdp;
this.personListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
}
WPF绑定方式的更多相关文章
- wpf中UserControl的几种绑定方式
我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...
- WPF快速入门系列(4)——深入解析WPF绑定
一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...
- WPF 绑定StaticResource到控件的方法
原文:WPF 绑定StaticResource到控件的方法 资源文件内的属性能否直接通过绑定应用到控件?答案是肯定的. 比如,我们要直接把下面的<SolidColorBrush x:Key=&q ...
- WPF绑定各种数据源之object数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- RegisterAttached 两种绑定方式
RegisterAttached 含义:使用指定的属性名称.属性类型和所有者类型注册附加属性 绑定方式:C#绑定.WPF绑定 例:需求DataViewModel为DataView的VM层,在DataV ...
- WPF 绑定父类属性
原文:WPF 绑定父类属性 1.绑定父控件的属性. <ContextMenu x:Key="ContextMenuColoum"> <MenuItem Heade ...
- WPF绑定(Binding)(4)
什么是绑定(Binding)? 在winform中, 我们常常会用到各种类型的赋值, 例如: button1.Text="Hello"; label.Text="Hell ...
- 对比MFC资源文件谈谈WPF布局方式
对比MFC资源文件谈谈WPF布局方式 MFC方式 对于传统的MFC基于UI的应用程序设计通常分两步走,首先是设计UI,使用的是RC文件,然后是代码文件,对RC文件进行操作,如下面Figure 1 的基 ...
- [Spring MVC] - SpringMVC的各种参数绑定方式
SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") publi ...
随机推荐
- LilyPad Arduino可穿戴技术和电子织物控制器板简介
LilyPad Arduino可穿戴技术和电子织物控制器板简介 第1章 LilyPad Arduino概览 作为本书的第一章,在这里将为读者介绍LilyPad Arduino相关的基础知识.例如,L ...
- pda完美结合开销售单据haohantech-安卓打票扫描PDA智能POS 发展现状如何,会取代传统电脑 代替手工开单 么?
现场开单时指业务员每天到终端客户的现场进行谈单和接单的动作流程,现场开单一般主要是集中在BCD类门店和一些批发零售小店 现场开单的优势:现场开单可以及时掌控终端客户情况,促进客情关系,提高产品的铺市率 ...
- Redis作为多个Windows服务运行配置方法
1.首先下载微软官方推荐的版本redis-2.8.12 2.开始配置,首先复制两份redis.conf并且重命名为redis6379.conf redis6380.conf 里面的配置节点相应的更改 ...
- Revit二次开发示例:Journaling
关于Revit Journal读写的例子. #region Namespaces using System; using System.Collections.Generic; using Sys ...
- POJ 1155 (树形DP+背包+优化)
题目链接: http://poj.org/problem?id=1155 题目大意:电视台转播节目.对于每个根,其子结点可能是用户,也可能是中转站.但是用户肯定是叶子结点.传到中转站或是用户都要花钱, ...
- 【BZOJ】1103: [POI2007]大都市meg
http://www.lydsy.com/JudgeOnline/problem.php?id=1103 题意:一棵n节点的树(1<=n<=250000),m条边(1<=m<= ...
- C#SortedList排序列表怎么样逆序输出
C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...
- hdu Can you find it
这道题也是道二分的题,主要有几个注意点: 1.两个数组的合并的问题,可以将a数组和b数组合并,这样可以降低时间复杂度. 2.二分查找的left和right的变化问题.之前这里一直wa...一定要是le ...
- Css3 - 动画旋转
旋转1: <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="U ...
- NOJ 1651 Red packet(二分)
[1651] Red packet 时间限制: 1000 ms 内存限制: 65535 K 问题描述 New Year is coming! Our big boss Wine93 will dist ...