I have me at a standstill. I found the code posted by Ray Burns to create a DependencyProperty for a TextBox that allows you to limit which characters can be deleted by the user. I modified it a bit to instead limit which characters can be inserted, and used this to create text fields that accept only numeric input (plus a decimal point).
This is great for typing with keyboard, paste, drag and drop, etc. The only problem occurs when setting up code with text. There, it allows you to enter non-numeric text, which in itself is not a problem. The problem is that after checking the value of the TextBox Text property after that, it says it's an empty string.
Here is some code to demonstrate what I mean. Simple WPF window:
<Window x:Class="TestApp.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:TestApp"
Title="Test" Height="125" Width="200">
<Canvas>
<TextBox x:Name="txtTest" Canvas.Left="10" Canvas.Top="10" Width="100" my:TextBoxRestriction.RestrictInsertTo=".0123456789"></TextBox>
<Button Canvas.Left="10" Canvas.Top="40" Click="Button_Click">Enter Text</Button>
<Button Canvas.Left="75" Canvas.Top="40" Click="Button_Click_1">Check Value</Button>
</Canvas>
</Window>
His code is:
using System;
using System.Windows;
namespace TestApp
{
public partial class Test : Window
{
public Test()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
txtTest.Text = "Test";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MessageBox.Show(txtTest.Text, "Length = " + txtTest.Text.Length.ToString());
}
}
}
And my modification of the Ray class:
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace TestApp
{
public class TextBoxRestriction : DependencyObject
{
public static string GetRestrictInsertTo(DependencyObject obj)
{
return (string)obj.GetValue(RestrictInsertToProperty);
}
public static void SetRestrictInsertTo(DependencyObject obj, string value)
{
obj.SetValue(RestrictInsertToProperty, value);
}
public static readonly DependencyProperty RestrictInsertToProperty = DependencyProperty.RegisterAttached("RestrictInsertTo", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
{
PropertyChangedCallback = (obj, e) =>
{
var box = (TextBox)obj;
box.TextChanged += (obj2, changeEvent) =>
{
var oldText = GetUnrestrictedText(box);
var allowedChars = GetRestrictInsertTo(box);
if (box.Text == oldText || allowedChars == null) return;
foreach (var change in changeEvent.Changes)
{
if (change.AddedLength > 0)
{
string added = box.Text.Substring(change.Offset, change.AddedLength);
if (added.Any(ch => !allowedChars.Contains(ch)))
{
var ss = box.SelectionStart;
var sl = box.SelectionLength;
box.Text = oldText;
box.SelectionStart = ss;
box.SelectionLength = sl;
}
}
}
SetUnrestrictedText(box, box.Text);
};
}
});
public static string GetUnrestrictedText(DependencyObject obj)
{
return (string)obj.GetValue(UnrestrictedTextProperty);
}
public static void SetUnrestrictedText(DependencyObject obj, string value)
{
obj.SetValue(UnrestrictedTextProperty, value);
}
public static readonly DependencyProperty UnrestrictedTextProperty = DependencyProperty.RegisterAttached("UnrestrictedText", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
{
DefaultValue = "",
PropertyChangedCallback = (obj, e) =>
{
var box = (TextBox)obj;
box.Text = (string)e.NewValue;
}
});
}
}
TextBox, , , , " " ; , " ", " " , , Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text , ). - ( , ), " " , .
- , ? - , .
!