简体   繁体   English

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

[英]Can I get the Type() of a bound object in C#/WPF (even if the bound value is null)?

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). 我需要找出绑定对象的Type,即使该值为null(这是我的问题所在)。

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. 我通过绑定到一个对象来评估绑定,然后使用该对象作为获取Type的方法,但即使值为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. 现在,如果我有一个WPF控件绑定到任何这些属性(让我们假设它们是依赖属性)我想获取属性的类型,即使该值为null。

So, I have a custom control that now has a Binding object that represents {Binding Name} for instance. 所以,我有一个自定义控件,现在有一个Binding对象,例如代表{Binding Name}。 How can I get the type of the "bound object" using C#? 如何使用C#获取“绑定对象”的类型?

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 . 如果是这样,我认为Binding有一个名为CreateBindingExpression的内部方法,它返回一个BindingExpression ,它有一个名为_listener的私有成员,内部类型为PropertyPathListener That has an internal property called LeafType , which I believe is what you're looking for. 它有一个名为LeafType的内部属性,我相信你正在寻找它。

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. 它很混乱,需要信任,并且在未来的Framework版本中会出现故障,但它可能是获得所需内容的唯一方法。

这应该只是一件事

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

If the value is null, there is no type to be gotten. 如果值为null,则没有要获取的类型。 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. 如果绑定是在App.xaml中定义的静态资源,那么你必须解析xaml文件本身以找出类型,如果它在类中定义,则必须反映它以找出类型。

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). 如果绑定是在代码中完成的,我不认为你可以这样做,因为它可以被绑定到一个你甚至无法反映出来的null局部变量(或者你可以,但那可能会结束我的头)。 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. 如果绑定是在xaml中定义的,那么您可以合理地解析xaml并尝试遵循解析其他xaml文件的xaml路径并反映绑定到代码中的路径的任何属性。

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. 这将是一个巨大的痛苦,我很确定无论你在这里的任何结果是什么,都可以通过做一些事情而不是试图识别类型,即使它是空的,也可以完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# WPF 如果绑定属性为 null,则绑定转换器不会触发 - C# WPF Binding Converter not firing if bound property is null 在 DataContext C# WPF 之外使用绑定的 CheckBox 值 - Use bound CheckBox value also outside of the DataContext C# WPF 如何在WPF C#中的数据绑定DataGrid中将字符串格式设置为货币格式? - How can i format a string as currency in a data bound DataGrid in WPF C#? WPF - 如何获取绑定到ListBoxItem的对象 - WPF - How do I get an object that is bound to a ListBoxItem back 如何将WPF UserControl挂钩到绑定的ViewModel对象的事件? - How can I hook a WPF UserControl to an event of a bound ViewModel object? 使用C#将单元格类型转换为DataGridViewCheckBoxCell并将其绑定到一个值 - Convert a cell type to DataGridViewCheckBoxCell using c# and bound it to a value C# 语言:如何获取绑定但打开的通用 class 类型? - C# Language: How to get type of bound but open Generic class? 获取C#中动态对象的属性类型(即使value为null) - Get property type of dynamic object in C# (even when value is null) WPF:UI不会在绑定对象的值更改时更新 - WPF: UI not updated on value change in bound object c# 获取 gridview 中绑定文本框的值(文本) - c# Get value (text) of a bound textbox in a gridview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM