WPF 获取一个控件某个依赖属性的默认绑定方式

参考:WPF控件的Binding.Mode=Default的默认值是什么?-腾讯云开发者社区-腾讯云

另外这里给一个案例,

我想通过:

cs 复制代码
FieldInfo field = typeof(RadNumericUpDown).GetField("ValueProperty",
    BindingFlags.Public | BindingFlags.Static);

结果发现field为null,

var a = new RadNumericUpDown();

var b = a.Value;

通过new一个新的RadNumericUpDown的对象,通过F12查看Value定义,发现定义在基类上:

cs 复制代码
public double? Value
 {
   get => (double?) this.GetValue(RadRangeBase.ValueProperty);
   set => this.SetValue(RadRangeBase.ValueProperty, (object) value);
 }

然后把代码改为:

cs 复制代码
// 从基类 RadRangeBase 获取
var field = typeof(RadRangeBase).GetField("ValueProperty",
    BindingFlags.Public | BindingFlags.Static);

if (field != null)
{
    var valueProperty = field.GetValue(null) as DependencyProperty;
    // 关键:将 metadata 转换为 FrameworkPropertyMetadata

    //PropertyMetadata是FrameworkPropertyMetadata的基类
    var metadata = valueProperty.GetMetadata(typeof(RadNumericUpDown))
        as FrameworkPropertyMetadata;

    if (metadata != null)
    {
        Console.WriteLine($"BindsTwoWayByDefault: {metadata.BindsTwoWayByDefault}");
        // 输出应该是: BindsTwoWayByDefault: True
    }

    //Console.WriteLine($"BindsTwoWayByDefault: {metadata.BindsTwoWayByDefault}");
}
相关推荐
samble4 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble12 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴13 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67514 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教14 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨16 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟17 天前
WPF(数据绑定)
wpf
samble18 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟18 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911121 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理