12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using Avalonia.Controls;
- using Avalonia.Input;
- using Avalonia.Interactivity;
- using Avalonia.Xaml.Interactivity;
- namespace Avalonia.Xaml.Interactions.Custom;
- /// <summary>
- /// A behavior that allows to hide control on lost focus event.
- /// </summary>
- public class HideOnLostFocusBehavior : StyledElementBehavior<Control>
- {
- /// <summary>
- /// Identifies the <seealso cref="TargetControl"/> avalonia property.
- /// </summary>
- public static readonly StyledProperty<Control?> TargetControlProperty =
- AvaloniaProperty.Register<HideOnLostFocusBehavior, Control?>(nameof(TargetControl));
- /// <summary>
- /// Gets or sets the target control. This is a avalonia property.
- /// </summary>
- [ResolveByName]
- public Control? TargetControl
- {
- get => GetValue(TargetControlProperty);
- set => SetValue(TargetControlProperty, value);
- }
- /// <inheritdoc />
- protected override void OnAttachedToVisualTree()
- {
- AssociatedObject?.AddHandler(InputElement.LostFocusEvent, AssociatedObject_LostFocus,
- RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
- }
- /// <inheritdoc />
- protected override void OnDetachedFromVisualTree()
- {
- AssociatedObject?.RemoveHandler(InputElement.LostFocusEvent, AssociatedObject_LostFocus);
- }
- private void AssociatedObject_LostFocus(object? sender, RoutedEventArgs e)
- {
- if (TargetControl is not null)
- {
- TargetControl.IsVisible = false;
- }
- }
- }
|