WPF中INotifyPropertyChanged用法与数据绑定

在WPF中进行数据绑定的时候常常会用到INotifyPropertyChanged接口来进行实现,下面来看一个INotifyPropertyChanged的案例。

下面定义一个Person类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1
{
    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private String _name = "张三";
        private int _age = 24;
        private String _hobby = "篮球";

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        public int Age
        {
            get
            {
                return _age;

            }
            set
            {
                _age = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                }
            }
        }

        public string Hobby
        {
            get { return _hobby; }
            set
            {
                _hobby = value;
                //if (PropertyChanged != null)
                //{
                //    PropertyChanged(this, new PropertyChangedEventArgs("Hobby"));
                //}
            }
        }
    }
}
 

上面定义的这个Person类中,对Name和Age属性进行了监听,但是没有对Hobby进行监听。

 

MainWindow.xmal界面文件定义的内容如下:

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="301.589" Width="249.924">
    <Grid x:Name="grid">
        <TextBox Height="20" Text="{Binding Path=Name}"  HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />
        <TextBox Height="20"  Text="{Binding Path=Age}"  HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />
        <TextBox Height="20" Text="{Binding Path=Hobby}"  HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />
        <Button Content="显示用户信息" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />
        <Button Content="修改用户信息" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />

        <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1"   Text="{Binding Path=Name}"  VerticalAlignment="Top" Width="63" />
        <TextBlock Height="40" HorizontalAlignment="Left" Margin="103,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="61" RenderTransformOrigin="0.102,0.658" />
        <TextBlock Height="40" HorizontalAlignment="Left" Margin="194,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="49" />
    </Grid>
</Window>


后台代码是:

using System;
using System.Collections.Generic;
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 WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private Person p1 = new Person();
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            grid.DataContext = p1;//绑定数据  
            p1.Name = "李四";
            //p1.Hobby = "足球";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            p1.Age = p1.Age + 1;
            p1.Hobby = "足球";
        }
    }
}


当点击显示用户数据的时候

 

下面看看这些信息具体都来自于哪儿?

由于在Person中没有对Hobby进行监听,所以p1.Hobby="足球"这个语句没有起到作用。 点击修改用户信息的时候也是不能修改绑定到界面上的对应Hobby的信息(即使是在界面处写了Mode=TwoWay,也是不能进行绑定的)。

所以使用INotifyPropertyChanged的时候,需要对要进行绑定的属性进行显示的设置的,否则绑定的时候是不能进行双向绑定的,即绑定是无效的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值