How to use reflection to get BindingExpression (and value) for controls that have ContentProperty in Silverlight

I need to use reflection to get the binding value in a control DataGridTemplateColumn(e.g. HyperLinkButton). Does anyone know how I can do this?

It seems simple enough to do this with TextBlock, because it has a dependency property TextProperty, but I cannot get the binding expression from a control that does not have a direct one TextProperty. Here's the Im code used to get the binding expression for 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, which is HyperLinkButton:

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

Does anyone know how I can get BindingExpression(and the binding value) for the content HyperLinkButton?

+3
source share
1 answer

Have you tried to add the correct anchor flags for this field? This sounds like a case of invalid snap flags when using reflection. TextBlock has a static Text field directly on a TextBlock, where, since HyperlinkButton has content inherited from ContentControl.

Try using the Static and Public and FlattenedHierarchy binding flags:

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

adding a FlattenHierarchy display binding flag should reflect a reflection in the class hierarchy to find this public static field.

+2
source

Source: https://habr.com/ru/post/1738104/


All Articles