ShowBehaviorBase.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Avalonia.Controls;
  2. using Avalonia.Interactivity;
  3. using Avalonia.Threading;
  4. namespace Avalonia.Xaml.Interactions.Custom;
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public abstract class ShowBehaviorBase : AttachedToVisualTreeBehavior<Control>
  9. {
  10. /// <summary>
  11. /// Identifies the <seealso cref="TargetControl"/> avalonia property.
  12. /// </summary>
  13. public static readonly StyledProperty<Control?> TargetControlProperty =
  14. AvaloniaProperty.Register<ShowBehaviorBase, Control?>(nameof(TargetControl));
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public static readonly StyledProperty<RoutingStrategies> EventRoutingStrategyProperty =
  19. AvaloniaProperty.Register<ShowBehaviorBase, RoutingStrategies>(nameof(EventRoutingStrategy), RoutingStrategies.Bubble);
  20. /// <summary>
  21. /// Gets or sets the target control. This is a avalonia property.
  22. /// </summary>
  23. [ResolveByName]
  24. public Control? TargetControl
  25. {
  26. get => GetValue(TargetControlProperty);
  27. set => SetValue(TargetControlProperty, value);
  28. }
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. public RoutingStrategies EventRoutingStrategy
  33. {
  34. get => GetValue(EventRoutingStrategyProperty);
  35. set => SetValue(EventRoutingStrategyProperty, value);
  36. }
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. /// <returns></returns>
  41. protected bool Show()
  42. {
  43. if (IsEnabled && TargetControl is { IsVisible: false })
  44. {
  45. TargetControl.SetCurrentValue(Visual.IsVisibleProperty, true);
  46. Dispatcher.UIThread.Post(() => TargetControl.Focus());
  47. return true;
  48. }
  49. return false;
  50. }
  51. }