首页 > 其他分享 >WPF.Basic.依赖属性

WPF.Basic.依赖属性

时间:2023-05-20 16:12:00浏览次数:38  
标签:propertyName storage System value Basic WPF property public 属性

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);
        }
    }
}

 

 

  

标签:propertyName,storage,System,value,Basic,WPF,property,public,属性
From: https://www.cnblogs.com/HomeSapiens/p/17417353.html

相关文章

  • Vue进阶(九十七):对象动态添加属性和值
    (文章目录)一、背景Vue规定不允许直接修改props或者data属性,必须使用Vue.set方法。Vue.set方法用来修改对象属性。如果要增加属性所属对象是响应式的,该方法可以确保属性被创建后也是响应式的,同时触发视图更新。二、基础知识注:修改的对象必须为响应式对象,且操作响应式对象的属......
  • Get-MMagent 是一个命令,通常用于查询与 Microsoft Management Agent (MMAgent) 相关的
    Get-MMagent是一个命令,通常用于查询与MicrosoftManagementAgent(MMAgent)相关的属性和配置信息。MMAgent是一款基于云计算技术的软件代理程序,用于帮助配置管理、安全性和监视方案。在Windows平台上,MMAgent通常用于实现高效的云端管理和自动化操作,包括AzureMonitor等相......
  • 九、属性分组--谷粒商城
    一、快速导入导航数据打开数据库gulimall_admin命令行界面, 导入sql文件 二、快速整合前端将modules下两个文件与自己的前端项目替换 项目接口文档:https://easydoc.net/s/78237135/ZUqEdvA4/4XuREIJe ......
  • 实体类属性映射
    //读取原实体属性为Name的值//fromEntity.GetType().GetProperty(name)获取实体的属性//GetValue(fromEntity)从fromEntity实体中取name属性值value=fromEntity.GetType().GetProperty(name).GetValue(fromEntity);//把取到的value值设置到toEntity实体中toEntity.GetTyp......
  • JS删除对象中的某一属性(delete)
    通过delete操作符,可以实现对对象属性的删除操作<!--*@Descripttion:删除对象以及数组对象中的指定属性*@version:*@Author:zhangfan*@email:[email protected]*@Date:2020-07-0309:10:28*@LastEditors:zhangfan*@LastEditTime:2020-07-1515:03:00......
  • 关于Apple设备私有的apple-touch-icon属性详解
    以前我们用过favicon在浏览器给网站进行身份标识,用法如下:1.<linkhref="http://image.feeliu.com/web/favicon.ico"rel="shortcuticon"/>2.<linkhref="http://image.feeliu.com/web/favicon.ico"rel="Bookmark"/> 现今移动设备越来越多,苹......
  • JS函数中的属性
    当定义和调用函数时,JavaScript函数对象会自动具有一些特定的属性,以下是一些常见的属性和方法。1.arguments:arguments是一个类数组对象,它包含了函数调用时传递的参数。它允许你在函数内部访问传递给函数的参数列表,即使在函数定义时未明确声明这些参数。可以通过索引访问argu......
  • wpf XAML 设计器异常,提示NullReferenceException 未将对象引用设置到对象
     在cs构造函数里手动注册,并且在控件的构造函数里增加判断if(DesignerProperties.GetIsInDesignMode(this)){return;}//在这里才注册Load事件cmbSpeed.Loaded+=cmbSpeed_Loaded;来源:https://www.cnblogs.com/zsx-blog/p/8311633.html ......
  • HTML属性 分为两种Property 固有属性Attribute 自定义属性
    HTML属性 分为两种Property固有属性Attribute自定义属性。固有属性就是浏览器给默认给html标签绑定上的属性。 操作固有属性固有属性可以通过对象.属性名这样方式来设置和获取值。什么是自定义属性自定义属性就是用户自己定义,在固有属性列表中没有的属性。获取自定义......
  • C# WPF 实现高频量化,自动运行。
    基于交易所编写的量化交易程序。由WPF和C#实现。改进版。再也不用时时刻刻盯盘了。 并非上图的思路所编写,仅供参考,思路由个人的想法异同。仅仅个人用途。不做商业用途。如下图所示,会在任务栏实时刷新价格,也可以mini窗口显示。由于存储限制,用了灰色的gif演示。都是现货的思路......