using System; using System.Diagnostics.CodeAnalysis; using Avalonia.Metadata; namespace Avalonia.Xaml.Interactivity; /// /// A base class for behaviors, implementing the basic plumbing of . /// /// The object type to attach to public abstract class StyledElementTrigger : StyledElementTrigger where T : AvaloniaObject { /// /// Gets the object to which this behavior is attached. /// public new T? AssociatedObject => base.AssociatedObject as T; /// /// Called after the behavior is attached to the . /// /// /// Override this to hook up functionality to the /// /// protected override void OnAttached() { base.OnAttached(); if (AssociatedObject is null && base.AssociatedObject is not null) { var actualType = base.AssociatedObject?.GetType().FullName; var expectedType = typeof(T).FullName; var message = $"AssociatedObject is of type {actualType} but should be of type {expectedType}."; throw new InvalidOperationException(message); } } }