环境

类代码

public class Person:INotifyPropertyChanged
{
private string name;
public string Name
{
get { return this.name; }
set
{
this.name = value;
notifyPropertyChanged("Name");
}
}
private int age;
public int Age
{
get { return this.age; }
set
{
this.age = value;
notifyPropertyChanged("Age");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void notifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

窗体代码

public partial class BindTest : Window
{
public Person per;
public BindTest()
{
InitializeComponent();
per = new Person { Age = 100, Name = "深入浅出WPF" };
Binding b = new Binding();
b.Source = per;
b.Path = new PropertyPath("Name");
BindingOperations.SetBinding(textbox, TextBox.TextProperty, b);
box.DataContext = per;
}

private void Button_Click(object sender, RoutedEventArgs e)
{

text.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = per });
}
}

XAML代码

<StackPanel >
<TextBox Margin="10" Height="40" x:Name="textbox"></TextBox>
<TextBox Margin="10" Height="40" x:Name="text"></TextBox>
<TextBox Margin="10" Height="40" Text="{Binding Name}" x:Name="box"></TextBox>
<Button Height="40" Click="Button_Click"></Button>
</StackPanel>

三种写法

1,

C#

per = new Person { Age = 100, Name = "深入浅出WPF" };

Binding b = new Binding();

b.Source = per;

b.Path = new PropertyPath("Name");

BindingOperations.SetBinding(textbox, TextBox.TextProperty, b);

Xamll

<TextBox Margin="10" Height="40" x:Name="textbox"></TextBox>

2

C#

text.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = per });

Xaml

<TextBox Margin="10" Height="40" x:Name="text"></TextBox>

3

C#

InitializeComponent();

box.DataContext = per;

Xaml

<TextBox Margin="10" Height="40" Text="{Binding Name}" x:Name="box"></TextBox>

其中Text="{Binding Name}"与Text="{Binding Path=Name}"

WPF之Binding的三种简单写法的更多相关文章

  1. 整理:WPF中Binding的几种写法

    原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...

  2. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  3. 三种简单的html网页自动跳转方法

    三种简单的html网页自动跳转方法,可以让你在打开一个html网页时自动跳转到其它的页面. 方法/步骤   <html> <head> <title>正在跳转< ...

  4. js中的三种函数写法

    js中的三种函数写法 <script type="text/javascript"> //普通的声明方式 function myFun(m,n){ alert(m+n) ...

  5. 表单模糊查询的三种简单方式(springboot-h2-mybatis)

    前几天运营提到说后台管理系统有几个地方想要模糊查询..   想了下是简单的,就是要注意以前方法的被调用情况,进行增量改动,以免牵一发而动全身.整理一波记录下(本次案例是按名字模糊查询学生信息). 三种 ...

  6. jquery 三种开始写法

    在 jquery 代码中使用 $(document).ready()时,位于其中的所有代码都会在 DOM 加载后立即执行 第一种(推荐)$(document).ready(function(){   ...

  7. UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)

    1.第一种方法:先定位到最后一行,若选中最后一行直接退出,否则用递归改变上次选中的状态,重新设置本次选中的状态. - (UITableViewCell*)tableView:(UITableView* ...

  8. java倒计时三种简单实现方式

    写完js倒计时,突然想用java实现倒计时,写了三种实现方式 一:设置时长的倒计时: 二:设置时间戳的倒计时: 三:使用java.util.Timer类实现的时间戳倒计时 代码如下: package ...

  9. Git的使用--如何将本地项目上传到Github(三种简单、方便的方法)

    一.第一种方法: 1.首先你需要一个github账号,所以还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路(傻瓜式安装) ...

随机推荐

  1. ByteBuffer用法总结

    转自:http://blog.csdn.net/mars5337/article/details/6576417 在NIO中,数据的读写操作始终是与缓冲区相关联的.读取时信道(SocketChanne ...

  2. Android UI ActionBar功能-ActionBarSherlock 的使用

    ActionBarSherlock实现了在ActionBar上添加一个下拉菜单的功能,也是App常用的功能之一: ActionBarSherlock是第三方提供的一个开源类库,下载地址:http:// ...

  3. python-多线程(原理篇)

    多线程的基本概念 语言学习总是绕不过一些东西,例如多进程和多线程,最近越来越发现,上来看几个实例练习一下过几天就不知其所以然了.所以还是先看看原理,在看实例练习吧! 线程的概念 概念:线程是进程中执行 ...

  4. php将文件夹打包成zip文件

    function addFileToZip($path,$zip){    $handler=opendir($path); //打开当前文件夹由$path指定.    while(($filenam ...

  5. 树形dp-hdu-4714-Tree2cycle

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4714 题目意思: 给一棵树,去掉一条边和增加一条边的花费都为1,求最小的花费,使该树变成一个环. 解 ...

  6. SQL 字段里有逗号隔开的数据的取值

    table 1 : id code1 0012 001,0023 001,002,003 table 2:code name001 数学002 体育003 美术 要求结果 id name1 数学2 数 ...

  7. Deep Clone 常用方式总结

    Deep Clone Example 总结 Deep Clone 一般有如下几种实现方式: 纯手工每个类实现赋值 (ps: 不做介绍,一般都不想这么玩) 序列化和反序列化 纯反射 emit 或 Exp ...

  8. ES6第一篇

    //新的数字方面的方法 const I = 3.4893589; console.log(Number.parseInt(I)); console.log(Number.parseFloat(I)); ...

  9. Linux学习之查看远端的端口是否通畅

    以80端口为例解答 法一:此法常被用来检测是个远端端口是否通畅 [root@oldboy ~]# telnet baidu.com Trying 123.125.114.144... Connecte ...

  10. 事件绑定之.bind()

    .bind(eventType[,eventData],handler(eventObject)) 描述:为一个元素绑定一个事件处理程序,bind()绑定方法的时候元素必须已经存在. -eventTy ...