@@ -182,7 +182,17 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
182182
183183 if ( Source is RelativeBindingSource relativeSource )
184184 {
185- ApplyRelativeSourceBinding ( relativeSource , bindObj , targetProperty , specificity ) ;
185+ var relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element ;
186+ if ( relativeSourceTarget is not Element )
187+ {
188+ var message = bindObj is not null
189+ ? $ "Cannot apply relative binding to { bindObj . GetType ( ) . FullName } because it is not a superclass of Element."
190+ : "Cannot apply relative binding when the target object is null." ;
191+
192+ throw new InvalidOperationException ( message ) ;
193+ }
194+
195+ ApplyRelativeSourceBinding ( relativeSource , relativeSourceTarget , bindObj , targetProperty , specificity ) ;
186196 }
187197 else
188198 {
@@ -192,13 +202,9 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
192202
193203#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
194204 async void ApplyRelativeSourceBinding (
195- RelativeBindingSource relativeSource , BindableObject targetObject , BindableProperty targetProperty , SetterSpecificity specificity )
205+ RelativeBindingSource relativeSource , Element relativeSourceTarget , BindableObject targetObject , BindableProperty targetProperty , SetterSpecificity specificity )
196206#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
197207 {
198- var relativeSourceTarget = RelativeSourceTargetOverride ?? targetObject as Element ;
199- if ( ! ( relativeSourceTarget is Element ) )
200- throw new InvalidOperationException ( ) ;
201-
202208 await relativeSource . Apply ( this , relativeSourceTarget , targetObject , targetProperty , specificity ) ;
203209 }
204210
0 commit comments