WPF和Expression Blend開發實例:一個樣式實現的數字輸入框
阿新 • • 發佈:2018-08-23
ref send 就是 prev work args msd extc span 原文:WPF和Expression Blend開發實例:一個樣式實現的數字輸入框
今天來一個比較奇淫技巧的手法,很少人用,同時也不推薦太過頻繁的使用.
先上樣式:
<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>
其實核心只有一個,就是xaml裏寫代碼.
x:Code Msdn介紹
引用樣式:
<TextBox Height="20" Width="200" Margin="10,0" Style="{StaticResource NumberTextBox}"/>
源代碼下載:
http://files.cnblogs.com/youngytj/TextBoxStyle.rar
WPF和Expression Blend開發實例:一個樣式實現的數字輸入框