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工具,这里给出下载地址,下载后一路(傻瓜式安装) ...
随机推荐
- #include <stdio.h>
1 fflush 2 fgetc 3 fgets 4 fprintf 5 fputc 6 fputs 7 fscanf 8 fseek 9 ftell 10 perror 11 remove 12 r ...
- vim简单命令教程-firstblood
你想以最快的速度学习人类史上最好的文本编辑器VIM吗?你先得懂得如何在VIM幸存下来,然后一点一点地学习各种戏法. Vim the Six Billion Dollar editor Better, ...
- poj 3020 Antenna Placement (最小路径覆盖)
链接:poj 3020 题意:一个矩形中,有n个城市'*'.'o'表示空地,如今这n个城市都要覆盖无线,若放置一个基站, 那么它至多能够覆盖本身和相邻的一个城市,求至少放置多少个基站才干使得全部的城市 ...
- 关于css3中before与after用单冒号还是双冒号的疑虑
在 CSS3 中为了区别伪元素和伪类为伪元素使用了双冒号,因此如果使用了 display 或者 width 等属性时使得显示脱离了原本元素后,建议按照标准双写.
- bootstrap注意事项(二)
1.内联子标题 在标题内还可以包含 <small> 标签或赋予 .small 类的元素,可以用来标记副标题. <!DOCTYPE html> <html> < ...
- SharePoint 2013 强制安装解决方案
Add-SPSolution Install-SPSolution -Identity DemonstrationZone.wsp -GACDeployment -CompatibilityLevel ...
- Asp.Net MVC 常用开发方式之EF Code First
在我们的工作和学习当中,经常会遇到中小型项目,这些项目除了业务上的区别较大外,对于底层和数据访问,其实都差不多.记得以前做项目时,每次都要重复的写底层操作数据库的代码,不仅浪费时间,也无太大意思,后来 ...
- JS 精粹(三)
(一)基本问题 JS的数据类型(不是数据结构)分:简单数据类型(undefined\null\boolean\string\number\symbol).复杂数据类型(object). 对象是可变的键 ...
- linux可执行文件目录的区别
/bin./sbin./usr/bin./usr/sbin目录的区别 在linux下我们经常用到的四个应用程序的目录是/bin./sbin./usr/bin./usr/sbin .而四者存放的文件一般 ...
- 动态include与静态include的区别
搬一下以前写的 个人总结: 动态INCLUDE 用jsp:include动作实现 <jsp:include page="included.jsp" flush="t ...