I want to get the Property Name (string) or PropertyInfo (object) of the binded proberty inside a TypedBinding....
For now I use this and it works only if parent doesn't have a x:DataType
private static MethodInfo _bindablePropertyGetContextMethodInfo = typeof(BindableObject).GetMethod("GetContext", BindingFlags.NonPublic | BindingFlags.Instance);
private static FieldInfo _bindablePropertyContextBindingFieldInfo;
public static Binding GetBinding(this BindableObject self, BindableProperty property)
object bindablePropertyContext = _bindablePropertyGetContextMethodInfo.Invoke(self, new[] { property });
if (bindablePropertyContext != null)
FieldInfo propertyInfo = _bindablePropertyContextBindingFieldInfo =
_bindablePropertyContextBindingFieldInfo ??
bindablePropertyContext.GetType().GetField("Binding");
var valued = propertyInfo.GetValue(bindablePropertyContext);
if (valued is Binding)
return (Binding)valued;
else return null;
return null;
if it has a datatype the valued is of type
{Xamarin.Forms.Internals.TypedBinding<ViewModelNameAndPath,string>}
And it can't be casted to Binding, if I cast to BaseBinding it doesn't have the Path property.... How can I achieve this ?
Thanks
I want to get the property name that was binded...
Let's say I bind a property Testo so I'll have a Step = {Binding Testo}, but on my control I wanted to know what was the real name of the property binded to my BindableProperty Step in this case Testo.
In my control i have a bindable property
public static readonly BindableProperty StepProperty =
BindableProperty.Create(propertyName: nameof(Step),
returnType: typeof(string),
declaringType: typeof(SsNumericEntry),
defaultValue: "");
I think it is quite difficult to know the exact name of the property binded inside the control..
Testo
does not have to be a bindable property, so the "real" name is the same name. It likely has a backing field [1] and calls OnPropertyChanged, but that is not really necessary if the data won't change at runtime.
string _testo;
public string Testo
get { return _testo; }
if (_testo!= value)
_testo= value;
OnPropertyChanged();
Thanks for reply,
I have changed the logic in that component. It was a reusable component that have bindable properties and I was binding another viewmodel to the component, so when I wanted to change the text OnPropertyChanged(); was not working because it was on a different viewmodel and I was trying to change with reflection the value inside another viewmodel ( the parent ). But I think this is a wrong pattern so I have changed all logic inside in order to not bind another viewmodel inside component.