我能否获取 C#/WPF 中绑定对象的 Type()(即使绑定值为 null)?

发布于 2024-09-15 10:35:33 字数 422 浏览 5 评论 0原文

我与未知来源有绑定。我所拥有的只是绑定。我没有其他方法来查看绑定对象。我需要找出绑定对象的类型,即使该值为空(这就是我的问题所在)。

我通过绑定到一个对象然后使用该对象作为获取类型的方式来评估绑定,但即使该值为 null,我也需要知道该类型。

例如,我有一个像这样的类:

public class Customer{
  public string Name { get; set; }
  public int Age { get; set; }
}

现在,如果我有一个 WPF 控件绑定到任何这些属性(假设它们是依赖属性),我想获取该属性的类型,即使该值为 null。

因此,我有一个自定义控件,它现在有一个代表 {Binding Name} 的 Binding 对象。如何使用 C# 获取“绑定对象”的类型?

I have a binding to an unknown source. All I have is the binding. I have no other way of looking at the bound object. I need to figure out the Type for the bound object, even if the value is null (this is where my problem is).

I was evaluating the binding by binding to an object and then using the object as a way to get the Type, but I need to know the type even if the value is null.

For instance, I have a class like so:

public class Customer{
  public string Name { get; set; }
  public int Age { get; set; }
}

Now, if I have a WPF control bind to any of those properties (let's assume they are dependency properties) I would like to get the type of the property, even if the value is null.

So, I have a custom control that now has a Binding object that represents {Binding Name} for instance. How can I get the type of the "bound object" using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

盛夏已如深秋| 2024-09-22 10:35:33

您愿意使用反射来访问非公开成员吗?如果是这样,我认为 Binding 有一个名为 CreateBindingExpression 的内部方法,它返回一个 BindingExpression,它有一个名为 _listener 的私有成员内部类型 PropertyPathListener 的 code>。它有一个名为 LeafType 的内部属性,我相信这就是您正在寻找的。

它很混乱,需要信任,并且在框架的未来版本中可能会失败,但这可能是获得您正在寻找的东西的唯一方法。

Are you willing to use reflection to get access to non-public members? If so, I think Binding has an internal method called CreateBindingExpression that returns a BindingExpression, which has a private member called _listener of internal type PropertyPathListener. That has an internal property called LeafType, which I believe is what you're looking for.

It's messy, requires trust, and is subject to failure in future versions of the Framework, but it might be the only way to get what you're looking for.

紫罗兰の梦幻 2024-09-22 10:35:33

这应该只是一个做的问题

MyCustomControl.DataContext != null ? MyCustomControl.GetType() : default(Type);

It should just be a matter of doing

MyCustomControl.DataContext != null ? MyCustomControl.GetType() : default(Type);
尤怨 2024-09-22 10:35:33

如果值为 null,则无法获取类型。如果绑定到 App.xaml 中定义的静态资源,则实际上必须解析 xaml 文件本身才能找出类型,如果它是在类中定义的,则必须反映它才能找出类型。

如果绑定是在代码中完成的,我认为你不能这样做,因为它可能会绑定到一个空局部变量,你甚至无法反映出来(或者也许你可以,但这就结束了)我的头)。如果绑定是在 xaml 中定义的,您可以合理地解析 xaml,并尝试遵循 xaml 路径解析其他 xaml 文件并反映该路径到代码中的绑定的任何属性。

这将是一个巨大的痛苦,我很确定无论你的最终目标是什么,都可以完成,而不需要花费可笑的时间,通过做一些除了尝试识别类型之外的事情,即使它是空的。

If the value is null, there is no type to be gotten. If the binding is to a static resource defined in the App.xaml, you would literally have to parse the xaml file itself to find out the type, if it's defined in a class you would have to reflect it to find out the type.

If the binding is done in code, I don't think you can do this, because it could be bound to a null local variable which you wouldn't even be able to reflect out (or maybe you can but that would be way over my head). If the binding is defined in xaml, you could rationally parse out the xaml and try to follow the xaml path parsing the other xaml files and reflecting any properties for bindings that path into the code.

This would be an enormous pain and I am pretty certain whatever you're endgoal here is, could be accomplished without the ridiculous time this would take by doing something other than trying to identify the type even if it was null.

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