如何获得 GetBindingExpression 目标反射
我可能需要一些帮助来解决我目前无法弄清楚的事情:
我需要在特定事件上为具有特定名称前缀的控件更新 XAML 绑定(通过类到不同的 resx 文件)。由于控件有不同的类型,而且我不知道同一页面在将来的某个时候会是什么样子,我想只用反射来做到这一点......
类似的东西
var meth1 = control.GetType().GetMethod("GetBindingExpression");
var meth2 = control.GetType().GetMethod("SetBinding");
BindingExpression be = (BindingExpression)meth1.Invoke(target, null);
Binding bind = be.ParentBinding;
meth2.Invoke(target, new object[] { bind });
对我来说似乎是正确的想法,但我无法想象知道如何在之前不知道 DependencyObject 类型的情况下从 DependencyObject 获取目标 DependencyProperty...
我很确定我在这里错过了一些相当简单的东西...
[编辑] 我知道我可以浏览控件,然后将从 ResourceManager 对象获得的新字符串输入到控件的例如文本属性中,但在这种情况下,我必须再次检查文本、标题等任何属性。 ..如果可能的话,反射对我来说似乎是更干净的方式。
i could need some help on something i can't figure out at this point:
I need to update XAML-Bindings (via a class to varying resx-files) on a certain event for controls with a specific name prefix. As the controls have different types and i do no know how the same page will look somewhen in the future, i'd like to do that with reflections only...
something like
var meth1 = control.GetType().GetMethod("GetBindingExpression");
var meth2 = control.GetType().GetMethod("SetBinding");
BindingExpression be = (BindingExpression)meth1.Invoke(target, null);
Binding bind = be.ParentBinding;
meth2.Invoke(target, new object[] { bind });
seems to be the right idea to me, but i cannot figure out how to get the target DependencyProperty from a DependencyObject without knowing the DependencyObject Type before...
I am pretty sure i am missing something rather easy here ...
[edit]
I know that i can go through the controls and just enter the new string i get from a ResourceManager Object into the e.g. Text-Property of the Control, but in that case i'd again have to check for Text, Header, whatever properties ... if possible, reflections only seem the cleaner way to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地使用以下方式,
You can simply use following way,
感谢有关转换为 FrameworkElement 的两个提示,我设法回到正轨:
从这里开始,我认为会把事情做好
Thanks to the both hints regarding the cast to FrameworkElement, i managed to get back on track:
From here on i think will get things done