The simplest task that can work is to bind to the OnTextChanged
event and change the text according to your rules.
<TextBox x:Name="TheText" TextChanged="OnTextChanged" MaxLength="4"/>
private void OnTextChanged(object sender, TextChangedEventArgs e) { if (TheText.Text.Length == 0) return; var text = TheText.Text; int result; var isValid = int.TryParse(text, out result); if (isValid) return; TheText.Text = text.Remove(text.Length - 1); TheText.SelectionStart = text.Length; }
However, I would shy away from this approach, as the Metro mantra touched the first user interface, and you can easily do it with one touch using FlipView
.
source share