环境

类代码

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. 基于Platinum库的DMS实现(android)

    接上篇博文:基于Platinum库的DMR实现(android) 文章讲述了如何使用Platinum库实现DMR 今天同样使用该库,来讲解一下DMS的实现 关于该库如何编译,请参考这篇博文:NDK下 ...

  2. How to change a product dropdown attribute to a multiselect in Magento

    First, update the attribute input type to multiselect: UPDATE eav_attribute SET entity_type_id ', at ...

  3. (转)iOS 开发,工程中混合使用 ARC 和非ARC

    [前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...

  4. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  5. The Tips of Success(成功的建议)

    1.Do one thing at a time,and do well. 2.Never forget to say "thanks". 3,Keep on going.Neve ...

  6. CSS3属性text-overflow(省略符)实战开发详解

    先看一下效果:   好了,看完了效果,现在正式开始今天的开发旅程吧!   首先我们先创建html页面,代码如下所示(红色文字即是我们Demo的主要内容): <!DOCTYPE html> ...

  7. Spring MVC JSON 实现JsonSerializer Date类型转换

    转载至:http://blog.csdn.net/lantianzhange/article/details/40920933 在Spring MVC中存在两大类的类型转换,一类是Json,一个是Sp ...

  8. C 运算符与表达式

    运算(操作)是对数据的加工.最基本的运算形式常常可以用一些简洁的符号来记忆,这些符号称为运算符或操作符.被运算的对象-数据称为运算量或操作数.表达式描述了对哪些数据.以什么顺序以及施加什么样的操作.运 ...

  9. python dbhelper(simple orm)

    # coding:utf- import pymysql class Field(object): pass class Expr(object): def __init__(self, model, ...

  10. Swift 断言

    assert(条件,"输出信息"); 如: let age=-1; assert(age>=0,"age要大于0");