先看看如下例子,不使用INotifyPropertyChanged接口的话会存在什么问题。
不使用INotifyPropertyChanged接口
在新项目中新建一个类叫做Person.cs,这个类里面有3个字段分别是姓名、年龄、爱好,然后将字段封装。代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication20190905B
{
class Person
{
private string _name;
public string Name
{
get {
return _name; }
set {
_name = value; }
}
private int _age;
public int Age
{
get {
return _age; }
set {
_age = value; }
}
private string _hobby;
public string Hobby
{
get {
return _hobby; }
set {
_hobby = value; }
}
}
}
然后是MainWindow.xaml代码,分别使用了单向绑定和双向绑定。
<Window x:Class="WpfApplication20190905B.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="grid1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height