访问无法访问的受保护属性TemplatedParent [英] Accessing inaccessible protected property TemplatedParent

查看:139
本文介绍了访问无法访问的受保护属性TemplatedParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我通过继承"MarkupExtension"并编写所需的代码在WPF中创建了自己的自定义绑定.
我想在Silverlight中创建相同的Binding,我注意到Silverlight 5现在也支持继承MarkupExtension.

很好!

在我需要使用RelativeSource来访问模板化父级中用于控件的内容之前,它一直处于工作状态,我在Generix.xaml文件中具有XAML标记.我以前曾为此使用过{{RelativeSource Mode = TemplatedParent}.

在WPF中,我可以在自定义绑定中访问属性"TemplatedParent".
在Silverlight中无法访问此属性(尽管在调试时可以从Visual Studio 2010的非公共成员"下看到此属性).

而且LogicalTreeHelper不存在,所以我无法解析控件的父元素.

因此,我陷入了困境……除非有人可以帮助我访问受保护的属性"TemplatedParent"(我认为它在FrameworkElement上),或者通过某种方式从LogicalTreeHelper替代项访问父级(模板化父级).

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?

推荐答案

您总是可以使用类似
You can always use something like
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()返回一个Object,因此您必须知道将其强制转换为什么.
也有一个SetValue().

通常,这种反射材料被认为是缓慢且容易出错的.因此,使用它需要您自担风险.通常,出于某种原因,私有财产是私有的.


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.


这篇关于访问无法访问的受保护属性TemplatedParent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆