假如我在WPF程序里有个TextBox,我怎么把他的Text属性与我在另一个程序集里的类定义的属性绑定在一起。要求是如果我修改类定义的属性,Text属性也跟着改,反之亦然。作者: 王运波 时间: 2014-5-12 21:36
1.自定义生成的persons类 继承INotifyPropertyChanged接口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace 数据绑定事件
{
class Persons:INotifyPropertyChanged
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set {_name = value; }
}
public int Age
{
get { return _age;}
set
{
_age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
2.定义事件public event PropertyChangedEventHandler PropertyChanged;,在值进行修改的set属性触发事件PropertyChanged(this, new PropertyChangedEventArgs("Age"));
3.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.Navigation;
using System.Windows.Shapes;
namespace 数据绑定事件
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Persons p = new Persons();