我需要使用反射来获取控件中的绑定值 DataGridTemplateColumn (例如 HyperLinkButton)。有人知道我怎么做吗?

似乎很简单 TextBlock 因为它有一个 TextProperty 依赖性属性,但我似乎无法从没有立即的控件中获得具有约束力的表达 TextProperty. 。这是我用来获取绑定表达式的代码 TextBlock:

FrameworkElement fe = (FrameworkElement)dependencyObj;
FieldInfo fi = fe.GetType().GetField("TextProperty");
BindingExpression bindingExpression = fe.GetBindingExpression((DependencyProperty)fi.GetValue(null))

但是,以下代码对依赖关系对象永远不起作用 HyperLinkButton:

FieldInfo fi = fe.GetType().GetField("ContentProperty");

有人知道我如何能够获得 BindingExpression (和绑定值) HyperLinkButton?

有帮助吗?

解决方案

您是否尝试为该字段添加正确的绑定标志?使用反射时,这听起来像是iNadaquate绑定标志的情况。 TextBlock在TextBlock上具有文本静态字段,其中Hyperlinkbutton具有从ContentControl继承的内容。

尝试使用静态和公共和扁平结构绑定标志:

FieldInfo fi = fe.GetType().GetField("ContentProperty", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

添加FlattenHierArchy Archition绑定标志应告诉反射以在类层次结构中查找以找到该公共静态字段。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top