using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors;

namespace InABox.WPF;

public class TextBoxIntegerMaskBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.PreviewTextInput += PreviewTextInput;
        base.OnAttached();
    }
    
    protected override void OnDetaching()
    {
        AssociatedObject.PreviewTextInput -= PreviewTextInput;
        base.OnDetaching();
    }
    
    private void PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        if (!int.TryParse(e.Text, out int _))
            e.Handled = true;
    }
    
}