90 lines
2.7 KiB
C#
90 lines
2.7 KiB
C#
//https://stackoverflow.com/questions/35324285/how-to-create-masked-textbox-like-windows-ip-address-fields
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
namespace DTS.Common.Controls
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for IPTextBox.xaml
|
|
/// </summary>
|
|
public partial class TestIDTextBox : UserControl
|
|
{
|
|
private static readonly List<Key> OtherAllowedKeys = new List<Key> { Key.Delete };
|
|
|
|
private bool _suppressAddressUpdate = false;
|
|
|
|
public TestIDTextBox()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
//FB 43400 added Clear method to reset the segments
|
|
public void Clear()
|
|
{
|
|
tbTestId.Text = string.Empty;
|
|
}
|
|
|
|
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
|
|
"Text", typeof(string), typeof(TestIDTextBox), new FrameworkPropertyMetadata(default(string), TextChanged)
|
|
{
|
|
BindsTwoWayByDefault = true
|
|
});
|
|
|
|
private static void TextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
var tb = dependencyObject as TestIDTextBox;
|
|
var text = e.NewValue as string;
|
|
|
|
if (text != null && tb != null)
|
|
{
|
|
tb._suppressAddressUpdate = true;
|
|
|
|
if (IsValidText(text))
|
|
{
|
|
tb.tbTestId.Text = text;
|
|
}
|
|
else
|
|
{
|
|
tb.tbTestId.Text = (string)e.OldValue;
|
|
}
|
|
tb._suppressAddressUpdate = false;
|
|
}
|
|
}
|
|
|
|
public string Text
|
|
{
|
|
get { return (string)GetValue(TextProperty); }
|
|
set { SetValue(TextProperty, value); }
|
|
}
|
|
|
|
|
|
private static bool IsValidText(string text)
|
|
{
|
|
var invalidChars = new List<char>();
|
|
invalidChars.AddRange(Path.GetInvalidFileNameChars());
|
|
invalidChars.AddRange(Path.GetInvalidPathChars());
|
|
invalidChars.Add('.');
|
|
var charArray = text.ToCharArray();
|
|
return !Array.Exists(charArray, x => invalidChars.Contains(x));
|
|
}
|
|
|
|
private void tbTestId_PreviewTextInput(object sender, TextCompositionEventArgs e)
|
|
{
|
|
e.Handled = !IsValidText(e.Text);
|
|
}
|
|
|
|
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
if ( !(sender is TextBox tb)) { return; }
|
|
e.Handled = !IsValidText(tb.Text);
|
|
}
|
|
}
|
|
}
|