123456789101112131415161718192021222324252627 |
- 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;
- }
-
- }
|