(轉載請註明來源:cnblogs coder-fang)ide
自定義number string textbox 在wpf中,能夠在xaml層控制是否爲空,長度限制等this
1. 建立textbox父類,使其具備相關依賴屬性:spa
public class TextboxBase:TextBox { public bool CanEmpty { get { return (bool)GetValue(CanEmptyProperty); } set { SetValue(CanEmptyProperty, value); ValidText(); } } public int MaxTextLength { get { return (int)GetValue(MaxTextLengthProperty); } set { SetValue(MaxTextLengthProperty, value); } } public bool Valid { get { return (bool)GetValue(ValidProperty); } set { SetValue(ValidProperty, value); } } public string InvalidText { get { return (string)GetValue(InvalidTextProperty); } set { SetValue(InvalidTextProperty, value); } } public static readonly DependencyProperty MaxTextLengthProperty = DependencyProperty.Register("MaxTextLength", typeof(int), typeof(TextboxBase)); public static readonly DependencyProperty ValidProperty = DependencyProperty.Register("Valid", typeof(bool), typeof(TextboxBase)); public static readonly DependencyProperty InvalidTextProperty = DependencyProperty.Register("InvalidText", typeof(string), typeof(TextboxBase)); public static readonly DependencyProperty CanEmptyProperty = DependencyProperty.Register("CanEmpty", typeof(bool), typeof(TextboxBase)); public delegate bool ValidDelegate(out string Invalidtext); public ValidDelegate ValidFunc; public TextboxBase() { this.TextChanged += TextboxBase_TextChanged; CanEmpty = false; MaxTextLength = Int32.MaxValue; ValidFunc = (out string invalidtext) => { invalidtext = ""; return true; }; } private void TextboxBase_TextChanged(object sender, TextChangedEventArgs e) { ValidText(); } public virtual bool ValidText() { if (!CanEmpty && Text == "" && DataContext != null) { Valid = false; InvalidText = "Can not be empty"; return false; } else if (Text.Length > MaxTextLength) { Valid = false; InvalidText = "Max length is " + MaxTextLength; return false; } else { InvalidText = null; Valid = true; return true; } } }
2. 建立Number string類:code
public class NumStrTextbox:TextboxBase { Regex regex = new Regex(@"^\d+$"); public NumStrTextbox() { this.PreviewKeyDown += NumStrTextbox_PreviewKeyDown; System.Windows.Input.InputMethod.SetIsInputMethodEnabled(this, false); } public override bool ValidText() { var ret = base.ValidText(); if (!ret) { return false; } if (!regex.IsMatch(Text)) { InvalidText = "Only numbers is allowed"; Valid = false; ret = false; } return false; } private void NumStrTextbox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (((e.Key >= Key.D0 && e.Key <= Key.D9) ||(e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)) &&!e.KeyboardDevice.IsKeyDown(Key.LeftShift)&& !e.KeyboardDevice.IsKeyDown(Key.LeftShift) ) { } else if (e.Key == Key.Back || e.Key == Key.Delete || (e.Key >= Key.Left && e.Key <= Key.Down) || e.Key == Key.Tab || (e.Key >= Key.End && e.Key <= Key.Home)) { } else { e.Handled = true; return; } } }
3. 建立textbox相關style,使其在異常有紅色提示信息blog
<Style TargetType="UITextbox:TextboxBase" x:Key="WPFUITextboxBaseStyle" > <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="VerticalContentAlignment" Value="Center"></Setter> <Setter Property="CaretBrush" Value="White"></Setter> <Setter Property="MinHeight" Value="25"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type UITextbox:TextboxBase}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Foreground="Salmon" Text="{TemplateBinding InvalidText}" Visibility="{TemplateBinding Valid,Converter={StaticResource TrueCollapseCvt}}"></TextBlock> <Border Grid.Row="1" x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Valid" Value="false"> <Setter Property="BorderBrush" Value="Red"></Setter> </Trigger> </Style.Triggers> </Style>
效果圖:get