如何通过依赖属性给张铁匠带两块铁?

摘要:WPF - 依赖属性 & 附加属性 回顾 - 属性 & 属性元素 & 对象元素C# -> 属性 public string Name {
WPF - 依赖属性 & 附加属性 回顾 - 属性 & 属性元素 & 对象元素 // C# -> 属性 public string Name { set => name = value; get => name; } <!-- WPF --> <控件> <!-- 属性元素 --> <控件.属性名> <!-- 对象元素 --> <属性类型 属性名="属性值"/> </控件.属性名> </控件> <!-- 示例代码 --> <Button> <Button.Background> <SolidColorBrush Color="Red"/> </Button.Background> </Button> 一.依赖属性 先放结论,留个印象 🍀依赖属性 —— 使用普通属性语法控制WPF中属性值的一种机制 1.定义 - 微软官方 通常,官方文档讲的很好,虽然我不知道他咕噜咕噜地在说什么 所以,我们对于理解概念,最好的方法就是请求中译中 依赖属性: 依赖属性是一种由 WPF 属性系统统一管理的属性(由DependencyProperty支持的属性), 其值并不直接存储在对象实例中,而是由属性系统根据多种输入源按优先级计算得出 依赖属性标识符: 它是 DependencyProperty 注册依赖属性时作为返回值的实例,然后存储为类的静态成员 与 WPF 属性系统交互的许多 API 使用依赖属性标识符作为参数 CLR"包装器":get和set对属性的实现 这些实现通过在GetValue和SetValue调用中使用依赖项属性标识符来整合该标识符, 使WPF属性系统为该属性提供支持 public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register( "IsSpinning", typeof(bool), typeof(MainWindow) ); public bool IsSpinning { get => (bool)GetValue(IsSpinningProperty); set => SetValue(IsSpinningProperty, value); } 2.定义 - 中译中后 在看定义之前,先来看两段代码,一个是C#中普通属性的示例代码,一个是WPF的依赖属性的示例代码 // C# -> 普通属性(CLR普通属性) public string Name { set => name = value; get => name; } /* ================================================================================= */ // WPF -> 依赖属性 // 输入propdp,然后连续按两下Tab键,VS会帮我们自动生成下面的这段代码 public int MyProperty { get { return (int)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. // This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(int), typeof(ownerclass), n
阅读全文