首页 > Windows开发 > 详细

WPF 基础 - Binding 的 数据更新提醒

时间:2021-02-24 23:26:03      阅读:26      评论:0      收藏:0      [点我收藏+]

WPF 作为一个专门的展示层技术,让程序员专注于逻辑层,让展示层永远处于逻辑层的从属地位;
这主要因为有 DataBinding 和配套的 Dependency Property 和 DataTemplate;

1. 数据更新提醒

Binding 是一种自动机制,当属性的值变化后属性要有能力通知 Binding,让 Binding 把变化传递给 UI 元素。

class Student : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }
}
BindingOperations.SetBinding(this.textboxName, TextBox.TextProperty, 
  new Binding() { Source = new Student(), Path = new PropertyPath("Name") }
);

//FramewordElement 对 BindingOperations.SetBinding 进行了封装,且 Binding 实例化时接受传入 Path,故:
this.textboxName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = new Student() });

WPF 基础 - Binding 的 数据更新提醒

原文:https://www.cnblogs.com/MichaelLoveSna/p/14441421.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!