This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExecuteCommandOnLostFocusBehavior
- Loading branch information
1 parent
886d60b
commit 218ec6b
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/Avalonia.Xaml.Interactions.Custom/ExecuteCommandOnLostFocusBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Reactive.Disposables; | ||
using Avalonia.Input; | ||
using Avalonia.Interactivity; | ||
|
||
namespace Avalonia.Xaml.Interactions.Custom; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class ExecuteCommandOnLostFocusBehavior : ExecuteCommandBehaviorBase | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="disposable"></param> | ||
protected override void OnAttachedToVisualTree(CompositeDisposable disposable) | ||
{ | ||
var dispose = AssociatedObject? | ||
.AddDisposableHandler( | ||
InputElement.LostFocusEvent, | ||
AssociatedObject_LostFocus, | ||
RoutingStrategies.Tunnel | RoutingStrategies.Bubble); | ||
|
||
if (dispose is not null) | ||
{ | ||
disposable.Add(dispose); | ||
} | ||
} | ||
|
||
private void AssociatedObject_LostFocus(object? sender, RoutedEventArgs e) | ||
{ | ||
if (e.Handled) | ||
{ | ||
return; | ||
} | ||
|
||
if (ExecuteCommand()) | ||
{ | ||
e.Handled = true; | ||
} | ||
} | ||
} |