Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I created my own custom Binding in WPF by inheriting "MarkupExtension" and writing the code I needed.
I wanted to create the same Binding in Silverlight, and I noticed that Silverlight 5 now supports inheriting the MarkupExtension as well.

Very well!

It worked as it shold until I needed to use the RelativeSource to access stuff in my templated parent for controls which I have the XAML markup for in the Generix.xaml file. I have used "{RelativeSource Mode=TemplatedParent} for this before.

In WPF, I could access a property "TemplatedParent" in my custom binding.
This property is not accessible in Silverlight (although I can see it from Visual Studio 2010, under "Non-Public members" when I debug).

And the LogicalTreeHelper does not exist, so I cannot parse the control's parent elements.

So I'm stuck...unless someone can help me access either the protected property "TemplatedParent" (I think it is on the FrameworkElement), or access parent (the templated parent) from an LogicalTreeHelper alternative somehow.

Help?
Posted

1 solution

You can always use something like
C#
object myObject = new object();
System.Reflection.PropertyInfo pi = myObject.GetType().GetProperty(
    "targetPropertyName",
    System.Reflection.BindingFlags.CreateInstance
    | System.Reflection.BindingFlags.NonPublic
);
pi.GetValue(myObject, null);

GetValue() returns an Object so you have to know what to cast it to.
There is a SetValue(), too.

In general, this reflection stuff is believed to be slow and error-prone. So use it at your own risk. Usually private properties are private for a reason.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900