private void HandleBinding(object instance, IXamlBinding binding) { if (typeof(IXamlBinding).GetTypeInfo().IsAssignableFrom(_xamlMember.XamlType.UnderlyingType.GetTypeInfo())) { var property = instance.GetType().GetRuntimeProperty(_xamlMember.Name); if (property == null || !property.CanWrite) { throw new InvalidOperationException( $"Cannot assign to '{_xamlMember.Name}' on '{instance.GetType()}"); } property.SetValue(instance, binding); } else { ApplyBinding(instance, binding); } }
private void ApplyBinding(object instance, IXamlBinding binding) { var perspexObject = instance as PerspexObject; var attached = _xamlMember as PerspexAttachableXamlMember; if (perspexObject == null) { throw new InvalidOperationException( $"Cannot bind to an object of type '{instance.GetType()}"); } PerspexProperty property; string propertyName; if (attached == null) { propertyName = _xamlMember.Name; property = PerspexPropertyRegistry.Instance.GetRegistered(perspexObject) .FirstOrDefault(x => x.Name == propertyName); } else { // Ensure the OwnerType's static ctor has been run. RuntimeHelpers.RunClassConstructor(attached.DeclaringType.UnderlyingType.TypeHandle); propertyName = attached.DeclaringType.UnderlyingType.Name + '.' + _xamlMember.Name; property = PerspexPropertyRegistry.Instance.GetRegistered(perspexObject) .Where(x => x.IsAttached && x.OwnerType == attached.DeclaringType.UnderlyingType) .FirstOrDefault(x => x.Name == _xamlMember.Name); } if (property == null) { throw new InvalidOperationException( $"Cannot find '{propertyName}' on '{instance.GetType()}"); } binding.Bind(perspexObject, property); }