WPF.Basic.依赖属性

发布时间 2023-05-20 16:08:07作者: 懒树懒

1 依赖属性定义

   在WPF界面的数据绑定中,为了能够使绑定源数据和绑定目标在变更后能够通知对方,.net在原来的属性之上设计了依赖属性

     所以支持绑定的属性本质上它都是封装后的依赖属性。那么也就是说, 只有依赖属性才可以进行绑定。  

 1 依赖属性使用

public class UserModel
    {
        public UInt32 UserID { get; set; }
        public string UserName { get; set; }
        public UInt32 UserAccount { get; set; }
    }
    public class UserDBModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
        private ObservableCollection<UserModel> userdb = new ObservableCollection<UserModel>();
        public ObservableCollection<UserModel> UserDB
        {
            get { return userdb; }
            set
            {
                userdb = value;
                OnPropertyChanged(new PropertyChangedEventArgs("UserDB"));
            }
        }
    }

 

定义一个Bindable抽象基类,直接继承这个基类也是可以的

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

namespace WPFPactice.ControlsValidation
{
    public abstract class BindableBase : INotifyPropertyChanged
    {
        //
        // 摘要:
        //     Occurs when a property value changes.
        public event PropertyChangedEventHandler PropertyChanged;

        //
        // 摘要:
        //     Checks if a property already matches a desired value. Sets the property and notifies
        //     listeners only when necessary.
        //
        // 参数:
        //   storage:
        //     Reference to a property with both getter and setter.
        //
        //   value:
        //     Desired value for the property.
        //
        //   propertyName:
        //     Name of the property used to notify listeners. This value is optional and can
        //     be provided automatically when invoked from compilers that support CallerMemberName.
        //
        // 类型参数:
        //   T:
        //     Type of the property.
        //
        // 返回结果:
        //     True if the value was changed, false if the existing value matched the desired
        //     value.
        protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(storage, value))
            {
                return false;
            }

            storage = value;
            RaisePropertyChanged(propertyName);
            return true;
        }

        //
        // 摘要:
        //     Checks if a property already matches a desired value. Sets the property and notifies
        //     listeners only when necessary.
        //
        // 参数:
        //   storage:
        //     Reference to a property with both getter and setter.
        //
        //   value:
        //     Desired value for the property.
        //
        //   propertyName:
        //     Name of the property used to notify listeners. This value is optional and can
        //     be provided automatically when invoked from compilers that support CallerMemberName.
        //
        //   onChanged:
        //     Action that is called after the property value has been changed.
        //
        // 类型参数:
        //   T:
        //     Type of the property.
        //
        // 返回结果:
        //     True if the value was changed, false if the existing value matched the desired
        //     value.
        protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(storage, value))
            {
                return false;
            }

            storage = value;
            onChanged?.Invoke();
            RaisePropertyChanged(propertyName);
            return true;
        }

        //
        // 摘要:
        //     Raises this object's PropertyChanged event.
        //
        // 参数:
        //   propertyName:
        //     Name of the property used to notify listeners. This value is optional and can
        //     be provided automatically when invoked from compilers that support System.Runtime.CompilerServices.CallerMemberNameAttribute.
        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        //
        // 摘要:
        //     Raises this object's PropertyChanged event.
        //
        // 参数:
        //   args:
        //     The PropertyChangedEventArgs
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            this.PropertyChanged?.Invoke(this, args);
        }
    }
}