How to get the underlying data type of a bound property?
For testing purposes I created a viewmodel 'Person' with a property 'Age' of type Int32, that is bound to a textbox's text property.
Is there something like ...
BindingOperations.GetBindingExpression(this, TextBox.TextProperty).PropertyType
or can this information only be retrieved by reflection?
myBinding.Source.GetType().GetProperty("Age").PropertyType
Edit: I have a custom textbox class, where I want to attach my own validationrules, converters ...
It would be great to get the information inside f.e. the 'load'-event of the textbox class.
if the property is bound to a specific datatype you need to set the value in the text box to a valid value for the property before it will update the viewmodel source
It seems that any validation errors stop the view model updating. Which I think is rubbish.
You could get the value inside the Convert method of a converter:
XAML
Not sure where you need access to the type, but it is available at that level, if you needed to transform the value.
The only way I have found to do this is by using reflection. The following code gets the binded object. It also handles nested binding
{Binding Parent.Value}
and if a converter is present - it returns the converted value. The method also returns the item's type for the cases in which the item is null.