Can I get Type () of a related object in C # / WPF (even if bound is null)?

I have a binding to an unknown source. All that I have is a must. I have no other way to look at a related object. I need to figure out the Type for the linked object, even if the value is null (this is my problem).

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

For example, I have a class:

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

Now, if I have a WPF control for any of these properties (let's say 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 example. How can I get the type of “linked object” using C #?

+3
source share
3 answers

Are you ready to use reflection to access non-public members? If so, I think it Bindinghas an internal method called CreateBindingExpressionthat returns BindingExpression, which has a private member with an _listenerinternal type name PropertyPathListener. This has an intrinsic property LeafTypethat I believe is what you are looking for.

, Framework, , .

+3

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

NULL, , . , App.xaml, xaml, , , , .

, , , , (, , , ). xaml, xaml xaml, xaml , .

That would be a huge pain, and I am pretty sure that you here, in the end, can be reached without the ridiculous time that it might take if you try to determine the type, even if it was null.

0
source

Source: https://habr.com/ru/post/1761125/


All Articles