mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using System;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Data;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Xaml.Interactivity;
|
|
|
|
namespace Artemis.UI.Shared.Behaviors;
|
|
|
|
/// <summary>
|
|
/// Represents a behavior that can be used to make a text box only update it's binding on focus loss.
|
|
/// </summary>
|
|
public class LostFocusTextBoxBindingBehavior : Behavior<TextBox>
|
|
{
|
|
public static readonly StyledProperty<string> TextProperty = AvaloniaProperty.Register<LostFocusTextBoxBindingBehavior, string>(
|
|
"Text", defaultBindingMode: BindingMode.TwoWay);
|
|
|
|
static LostFocusTextBoxBindingBehavior()
|
|
{
|
|
TextProperty.Changed.Subscribe(e => ((LostFocusTextBoxBindingBehavior) e.Sender).OnBindingValueChanged());
|
|
}
|
|
|
|
public string Text
|
|
{
|
|
get => GetValue(TextProperty);
|
|
set => SetValue(TextProperty, value);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnAttached()
|
|
{
|
|
if (AssociatedObject != null)
|
|
AssociatedObject.LostFocus += OnLostFocus;
|
|
base.OnAttached();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnDetaching()
|
|
{
|
|
if (AssociatedObject != null)
|
|
AssociatedObject.LostFocus -= OnLostFocus;
|
|
base.OnDetaching();
|
|
}
|
|
|
|
private void OnLostFocus(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (AssociatedObject != null)
|
|
Text = AssociatedObject.Text;
|
|
}
|
|
|
|
private void OnBindingValueChanged()
|
|
{
|
|
if (AssociatedObject != null)
|
|
AssociatedObject.Text = Text;
|
|
}
|
|
} |