WPF.Basic.依赖属性
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); } } }
重复就是力量,数量堆死质量
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!