如何使用反射来获取 Silverlight 中具有 ContentProperty 的控件的 BindingExpression(和值)
我需要使用反射来获取 DataGridTemplateColumn
控件中的绑定值(例如 HyperLinkButton
)。有谁知道我该怎么做?
使用 TextBlock
执行此操作似乎很简单,因为它具有 TextProperty
依赖属性,但我似乎无法从不具有 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");
有谁知道我如何才能获取 HyperLinkButton
内容的 BindingExpression
(和绑定值)?
I need to use reflection to get the binding value in a control that is a DataGridTemplateColumn
(e.g HyperLinkButton
). Does anyone know how I might do this?
It seems simple enough to do this with a TextBlock
because it has a TextProperty
dependency property, but I can’t seem to get a binding expression from a control that does not have an immediate TextProperty
. Here is the code I’m using to acquire the binding expression for a TextBlock
:
FrameworkElement fe = (FrameworkElement)dependencyObj;
FieldInfo fi = fe.GetType().GetField("TextProperty");
BindingExpression bindingExpression = fe.GetBindingExpression((DependencyProperty)fi.GetValue(null))
However, the following code never works for a dependency object that is a HyperLinkButton
:
FieldInfo fi = fe.GetType().GetField("ContentProperty");
Does anyone know how I might be able to get the BindingExpression
(and binding value) for the content of a HyperLinkButton
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过为该字段添加正确的绑定标志?这听起来像是使用反射时绑定标志不足的情况。
TextBlock 在 TextBlock 上有一个 Text 静态字段,而 HyperlinkButton 则具有从 ContentControl 继承的 Content。
尝试使用静态和公共& FlattenedHierarchy 绑定标志:
添加 FlattenHierarchy 反射绑定标志应该告诉反射在类层次结构中查找以查找公共静态字段。
have you tried adding the correct binding flags for that field? It sounds like a case of inadaquate binding flags when using reflection.
TextBlock has a the Text static field right on TextBlock, where as HyperlinkButton has Content inherited from ContentControl.
Try using the Static & Public & FlattenedHierarchy binding flags:
adding the FlattenHierarchy reflection binding flag should tell reflection to look up in the class hierarchy to find that public static field.