-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger textbox changes in settings on lost focus (#289)
- Loading branch information
Showing
4 changed files
with
106 additions
and
16 deletions.
There are no files selected for viewing
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,63 @@ | ||
using System; | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Data; | ||
using Avalonia.Interactivity; | ||
using Avalonia.Xaml.Interactivity; | ||
|
||
namespace LightBulb.Behaviors; | ||
|
||
public class LostFocusUpdateBindingBehavior : Behavior<TextBox> | ||
{ | ||
public static readonly StyledProperty<string?> TextProperty = AvaloniaProperty.Register< | ||
LostFocusUpdateBindingBehavior, | ||
string? | ||
>(nameof(Text), defaultBindingMode: BindingMode.TwoWay); | ||
|
||
static LostFocusUpdateBindingBehavior() | ||
{ | ||
TextProperty.Changed.Subscribe(args => | ||
{ | ||
if (args.Sender is LostFocusUpdateBindingBehavior behavior) | ||
behavior.OnBindingValueChanged(); | ||
}); | ||
} | ||
|
||
public string? Text | ||
{ | ||
get => GetValue(TextProperty); | ||
set => SetValue(TextProperty, value); | ||
} | ||
|
||
protected override void OnAttached() | ||
{ | ||
if (AssociatedObject is null) | ||
return; | ||
|
||
AssociatedObject.LostFocus += OnLostFocus; | ||
base.OnAttached(); | ||
} | ||
|
||
protected override void OnDetaching() | ||
{ | ||
if (AssociatedObject is null) | ||
return; | ||
|
||
AssociatedObject.LostFocus -= OnLostFocus; | ||
base.OnDetaching(); | ||
} | ||
|
||
private void OnLostFocus(object? sender, RoutedEventArgs e) | ||
{ | ||
if (AssociatedObject is null) | ||
return; | ||
|
||
Text = AssociatedObject.Text; | ||
} | ||
|
||
private void OnBindingValueChanged() | ||
{ | ||
if (AssociatedObject != null) | ||
AssociatedObject.Text = Text; | ||
} | ||
} |
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
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
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