WPF之Binding的三种简单写法
环境
类代码
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的三种简单写法的更多相关文章
- 整理:WPF中Binding的几种写法
原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...
- 三种简单的html网页自动跳转方法
三种简单的html网页自动跳转方法,可以让你在打开一个html网页时自动跳转到其它的页面. 方法/步骤 <html> <head> <title>正在跳转< ...
- js中的三种函数写法
js中的三种函数写法 <script type="text/javascript"> //普通的声明方式 function myFun(m,n){ alert(m+n) ...
- 表单模糊查询的三种简单方式(springboot-h2-mybatis)
前几天运营提到说后台管理系统有几个地方想要模糊查询.. 想了下是简单的,就是要注意以前方法的被调用情况,进行增量改动,以免牵一发而动全身.整理一波记录下(本次案例是按名字模糊查询学生信息). 三种 ...
- jquery 三种开始写法
在 jquery 代码中使用 $(document).ready()时,位于其中的所有代码都会在 DOM 加载后立即执行 第一种(推荐)$(document).ready(function(){ ...
- UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)
1.第一种方法:先定位到最后一行,若选中最后一行直接退出,否则用递归改变上次选中的状态,重新设置本次选中的状态. - (UITableViewCell*)tableView:(UITableView* ...
- java倒计时三种简单实现方式
写完js倒计时,突然想用java实现倒计时,写了三种实现方式 一:设置时长的倒计时: 二:设置时间戳的倒计时: 三:使用java.util.Timer类实现的时间戳倒计时 代码如下: package ...
- Git的使用--如何将本地项目上传到Github(三种简单、方便的方法)
一.第一种方法: 1.首先你需要一个github账号,所以还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路(傻瓜式安装) ...
随机推荐
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...
- ASP.NET MVC 学习之路-3
本文在于巩固基础 到这里不得不说ASP.NET MVC一个规则:惯例优先原则 ASP.NET会假定开发人员遵循特定的规则来构建自己的程序而不是使用配置文件 ASP.NET MVC文件夹结构也遵循惯例优 ...
- 为SharePoint 2010中的FBA创建自定义登录页面
SharePoint 2010中默认的FBA登录页面非常简单,只提供了一个Asp.Net的Login控件,让用户输入用户名和密码.在大多数情况下,我们需要定制这个页面以满足一些安全需求,比如为登录页面 ...
- Node.js(转) -- 临时来说还看不懂!
转自:http://blog.jobbole.com/53736/ 本文由 伯乐在线 - Lellansin 翻译.未经许可,禁止转载!英文出处:toptal.欢迎加入翻译组. 介绍 JavaScri ...
- CFILE追加写入文件
CFile file; file.Open(strName, CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate); ) { file.S ...
- css3实现三角形,聊天背景气泡,心形等形状
1.聊天背景气泡: css代码如下: #talkbubble {width: 120px;margin:auto; background: red; position: relative; -moz- ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- SingleNumber python实现
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- java 执行linux命令
原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...
- php命名空间使用
对于命名空间,官方文档已经说得很详细[查看],我在这里做了一下实践和总结. 命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只 ...