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 ...
随机推荐
- 【LINUX/UNIX网络编程】之使用消息队列,信号量和命名管道实现的多进程服务器(多人群聊系统)
RT,使用消息队列,信号量和命名管道实现的多人群聊系统. 本学期Linux.unix网络编程的第三个作业. 先上实验要求: 实验三 多进程服务器 [实验目的] 1.熟练掌握进程的创建与终止方法: 2 ...
- python 代码片段2
#coding=utf-8 # python没有花括号.我们用对齐来代替括号:通常是四个空格(任意数目的空格货tab也都可以) # python 注释用 # # python不需要像其他语言那样需要首 ...
- BZOJ1707 : [Usaco2007 Nov]tanning分配防晒霜
S向每头奶牛连边,容量1 每个防晒霜向T连边,容量cover 每头奶牛向SPF在自己范围内的防晒霜连边,容量inf 用线段树优化建图跑最大流即可. #include<cstdio> con ...
- BZOJ3833 : [Poi2014]Solar lamps
首先旋转坐标系,将范围表示成矩形或者射线 如果范围是一条线,则将灯按y坐标排序,y坐标相同的按x坐标排序, 对于y相同的灯,f[i]=min(i,它前面灯发光时刻的第k[i]小值), 线段树维护,$O ...
- BZOJ2584 : [Wc2012]memory
xy方向分开考虑 用扫描线处理出拓扑序,第二问直接回答拓扑序, 第一问: 将操作倒过来,变成加入线段,用线段树维护区间拓扑序的最值 #include<cstdio> #include< ...
- Codeforces Round #208 (Div. 2) B Dima and Text Messages
#include <iostream> #include <algorithm> #include <string> using namespace std; in ...
- 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...
- Java Web网站应用中的单点登录
采用SSH架构加以说明:1. 建立一个登录管理类LoginManager2. 在LoginManager中定义一个集合,管理登录的用户.3. 在Spring中将LoginManager配置成单例 ...
- iOS5中UIViewController的新方法
iOS5中UIViewController的新方法 前言 在苹果的 WWDC2011 大会视频的<Session 101 - What's New in Cocoa> 和<Sessi ...
- iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]
自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...