如何获得 GetBindingExpression 目标反射

发布于 2024-12-27 11:18:28 字数 712 浏览 3 评论 0原文

我可能需要一些帮助来解决我目前无法弄清楚的事情:

我需要在特定事件上为具有特定名称前缀的控件更新 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 技术交流群。

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

发布评论

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

评论(2

栖迟 2025-01-03 11:18:28

您可以简单地使用以下方式,

FrameworkElement fe = control as FrameworkElement;
foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(control))
{
   FieldInfo field = control.GetType().GetField(pd.Name + "Property");
   if(field == null)
       continue;
   DependencyProperty dp = field.GetValue(control) as DependencyProperty;
   if(dp == null)
       continue;
   BindingExpression be = control.GetBindingExpression(dp);
   if(be == null)
       continue;

   // do your stuff here

}

You can simply use following way,

FrameworkElement fe = control as FrameworkElement;
foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(control))
{
   FieldInfo field = control.GetType().GetField(pd.Name + "Property");
   if(field == null)
       continue;
   DependencyProperty dp = field.GetValue(control) as DependencyProperty;
   if(dp == null)
       continue;
   BindingExpression be = control.GetBindingExpression(dp);
   if(be == null)
       continue;

   // do your stuff here

}
若相惜即相离 2025-01-03 11:18:28

感谢有关转换为 FrameworkElement 的两个提示,我设法回到正轨:

foreach (var f in control.GetType().GetFields())
    {
        DependencyProperty dp = f.GetValue(control) as DependencyProperty;
        if (dp != null)
        {
            BindingExpression be = ((FrameworkElement)control).GetBindingExpression(dp);
            if (be != null)
            {
                // stuff here
            }
        }
     }

从这里开始,我认为会把事情做好

Thanks to the both hints regarding the cast to FrameworkElement, i managed to get back on track:

foreach (var f in control.GetType().GetFields())
    {
        DependencyProperty dp = f.GetValue(control) as DependencyProperty;
        if (dp != null)
        {
            BindingExpression be = ((FrameworkElement)control).GetBindingExpression(dp);
            if (be != null)
            {
                // stuff here
            }
        }
     }

From here on i think will get things done

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