Get Binded Property Name - TypedBinding

vanKraster 201 Reputation points
2021-12-16T14:14:11.7+00:00

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

Developer technologies .NET Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.