WPF数据绑定,参考代码
2015-06-29 10:09
357 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Wpf数据绑定; namespace Wpf数据绑定 { /// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { MainWindow wd; //构建函数传值 //public Window1(MainWindow w) //{ // InitializeComponent(); // wd = w; // aa.Text = wd.aaa.Text+wd.bbb.Password ; 用窗口传值 //} User1 us; //对象实例化 public Window1(User1 w) //用这个类接收新对象 { InitializeComponent(); us = w; aa.DataContext = w; //获取或设置参与数据绑定时的数据的上下文。和前台进行绑定 bb.SetBinding(TextBox.TextProperty, //不需要和前台进行绑定,这一局代码就可是实现并显示 new Binding("Pass") { Source = us, Mode = BindingMode.TwoWay }); //aa.Text = w.Username;//这句代码为什么不行呢?????? } private void Button_Click_1(object sender, RoutedEventArgs e) { us.Username = DateTime.Now.ToString("HH:mm:ss.fff"); } } }
类里的代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace Wpf数据绑定 { public class User1 : INotifyPropertyChanged //一个接口 { private string code; public string Code { get { return code; } set { code = value; } } private string username; public string Username { get { return username; } set { username = value; OnPropertyChanged("Username"); //调用下面的方法 } } private string pass; public string Pass { get { return pass; } set { pass = value; } } public event PropertyChangedEventHandler PropertyChanged; //用此种方法实现接口 public virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
绑定的代码
相关文章推荐
- Eclipse jar包不能放在一个文件夹
- DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1
- 让我们一起Go(五)
- 让我们一起Go(四)
- Java POI Word 写文档
- poi读写word模板 / java生成word文档
- 让我们一起Go(三)
- 让我们一起Go(二)
- [PHP]使用日志进行调试
- Ruby Profiler 详解之 ruby-prof(I)
- lua学习--记录(2)
- 从4行代码看右值引用
- 让我们一起Go(一)
- php数组函数总结-使用技巧(一)
- Java学习笔记:JFrame与Frame的区别
- java---使用日志输出信息的方法
- YII分页类 paging[CLinkPager]
- DevExpress.Web.ASPxGridView
- about ContentProvider
- Python访问纯真IP数据库脚本分享