一、实现INotifyPropertyChanged接口的Student类
class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get { return name; }
set
{
name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs("Name"));
}
}
}
}
二、界面设计
<Window x:Class="Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Binding"
mc:Ignorable="d"
Title="MainWindow" Height="100" Width="150">
<StackPanel>
<TextBox x:Name="text1" Height="23" BorderBrush="Black" Margin="4"/>
<Button x:Name="button" Content="Add Age" Margin="4" Click="button_Click"/>
</StackPanel>
</Window>
三、关联数据源和ui元素
BindingOperations.SetBinding(this.text1, TextBox.TextProperty, binding);
创建新实例,并将其与指定的绑定目标属性关联
namespace Student_Binding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Student stu;
public MainWindow()
{
InitializeComponent();
//准备数据源
stu = new Student();
//准备Binding
Binding binding = new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name");
//使用Binding连接数据源与Binding目标
//创建新实例,并将其与指定的绑定目标属性关联
BindingOperations.SetBinding(this.text1, TextBox.TextProperty, binding);
}
private void button_Click(object sender, RoutedEventArgs e)
{
stu.Name += "name";
}
}
四、完整代码
MainWindow.xaml
<Window x:Class="Student_Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Student_Binding"
mc:Ignorable="d"
Title="MainWindow" Height="100" Width="150">
<StackPanel>
<TextBox x:Name="text1" Height="23" BorderBrush="Black" Margin="4"/>
<Button x:Name="button" Content="Add Age" Margin="4" Click="button_Click"/>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Student_Binding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Student stu;
public MainWindow()
{
InitializeComponent();
//准备数据源
stu = new Student();
//准备Binding
Binding binding = new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name");
//使用Binding连接数据源与Binding目标
//创建新实例,并将其与指定的绑定目标属性关联
BindingOperations.SetBinding(this.text1, TextBox.TextProperty, binding);
}
private void button_Click(object sender, RoutedEventArgs e)
{
stu.Name += "name";
}
}
class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get { return name; }
set
{
name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
}