WPF and Expression Blend development example: a digital input box for style implementation, wpfblend
Today, we have a very odd method of obscenity, which is rarely used. We do not recommend that you use it too frequently.
First style:
<Style x:Key="NumberTextBox" TargetType="{x:Type FrameworkElement}"> <EventSetter Event="PreviewTextInput" Handler="TextBox_TextInput"/> <Setter Value="False" Property="InputMethod.IsInputMethodEnabled"/> </Style> <x:Code> <![CDATA[ private void TextBox_TextInput(object sender, TextCompositionEventArgs e) { bool flag = true; foreach (char c in e.Text) { if (c < '0' || c > '9') { flag = false; } } e.Handled = !flag; } ]]> </x:Code>
In fact, there is only one core, namely writing code in xaml.
X: Code Msdn Introduction
Reference style:
<TextBox Height="20" Width="200" Margin="10,0" Style="{StaticResource NumberTextBox}"/>
Source code download:
Http://files.cnblogs.com/youngytj/TextBoxStyle.rar