如何使用反射来获取 Silverlight 中具有 ContentProperty 的控件的 BindingExpression(和值)

发布于 2024-08-26 07:46:10 字数 788 浏览 11 评论 0原文

我需要使用反射来获取 DataGridTemplateColumn 控件中的绑定值(例如 HyperLinkBut​​ton)。有谁知道我该怎么做?

使用 TextBlock 执行此操作似乎很简单,因为它具有 TextProperty 依赖属性,但我似乎无法从不具有 TextProperty 依赖属性的控件中获取绑定表达式直接的TextProperty。这是我用来获取 TextBlock 的绑定表达式的代码:

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

但是,以下代码永远不适用于作为 HyperLinkBut​​ton 的依赖项对象:

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

有谁知道我如何才能获取 HyperLinkBut​​ton 内容的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-09-02 07:46:10

您是否尝试过为该字段添加正确的绑定标志?这听起来像是使用反射时绑定标志不足的情况。
TextBlock 在 TextBlock 上有一个 Text 静态字段,而 HyperlinkBut​​ton 则具有从 ContentControl 继承的 Content。

尝试使用静态和公共& FlattenedHierarchy 绑定标志:

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

添加 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:

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

adding the FlattenHierarchy reflection binding flag should tell reflection to look up in the class hierarchy to find that public static field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文