更新时间:2023-09-24 20:02:40
我建议您在下面的示例代码中添加另一个Dependency属性,我将其命名为Value还可以用逗号或NumberFormatInfo.CurrentInfo.NumberDecimalSeparator格式化您的电话号码并通过两个属性SelectionLength和SelectionStart控制插入符的位置更改.最后,有关更多详细信息和完整代码,请参见 WPF可屏蔽数字输入文本框
公共静态只读DependencyProperty ValueProperty =DependencyProperty.Register("Value",typeof(double),typeof(NumericTextBox),new PropertyMetadata(new Double(),OnValueChanged));私有静态无效OnValueChanged(DependencyObject sender,DependencyPropertyChangedEventArgs args){//var numericBoxControl =(NumericTextBox)sender;}公共双重价值{get {return(double)GetValue(ValueProperty);}设置{SetValue(ValueProperty,value);文字= value.ToString("###,###,###");}}
受保护的重写void OnPreviewTextInput(TextCompositionEventArgs e){base.OnPreviewTextInput(e);var txt = e.Text.Replace(,",");e.Handled =!IsTextAllowed(txt);如果(IsTextAllowed(txt)){如果(Text.Length == 3){文字= Text.Insert(1,,");SelectionLength = 1;SelectionStart + = Text.Length;}}}受保护的重写void OnPreviewKeyDown(KeyEventArgs e){base.OnPreviewKeyDown(e);如果(e.Key == Key.Back){如果(Text.Length == 5){文字= Text.Replace(,",");SelectionLength = 1;SelectionStart + = Text.Length;}}}受保护的重写void OnTextChanged(TextChangedEventArgs e){var txt = Text.Replace(,",");SetValue(ValueProperty,txt.Length == 0?0:double.Parse(txt));base.OnTextChanged(e);}私人静态布尔IsTextAllowed(字符串文本){尝试{double.Parse(text);返回true;}捕获(FormatException){返回false;}}
I want create a custom control for Numeric Text box with dependency property in WPF , in my solution , I add one WPF application and custom control (WPF) ,then in public class , I create dependency property ....
Now I don't know how can i write my rule for text box and which event is true?
Another question : What is my rule for numeric text box , that this text box must be give number and . and Separating .this custom Text box is for accounting system.
public static readonly DependencyProperty NumberTextbox;
static Numeric()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Numeric), new FrameworkPropertyMetadata(typeof(Numeric)));
FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata("Enter Your Text", OnKeyDown);
NumberTextbox =DependencyProperty.Register("Text", typeof(TextBox), typeof(FrameworkElement), metadata);
}
public string NumberTXT
{
get { return (string)GetValue(NumberTextbox); }
set { SetValue(NumberTextbox, value); }
}
I recommend to you add another Dependency Property in example code below I named it Value Also format your number by comma or NumberFormatInfo.CurrentInfo.NumberDecimalSeparator and control caret location changes by two property SelectionLength and SelectionStart. Finally for more detail and complete code WPF Maskable Number Entry TextBox
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(NumericTextBox), new PropertyMetadata(new Double(), OnValueChanged));
private static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
//var numericBoxControl = (NumericTextBox)sender;
}
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); Text = value.ToString("###,###,###"); }
}
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
base.OnPreviewTextInput(e);
var txt = e.Text.Replace(",", "");
e.Handled = !IsTextAllowed(txt);
if (IsTextAllowed(txt))
{
if (Text.Length == 3)
{
Text = Text.Insert(1,",");
SelectionLength = 1;
SelectionStart += Text.Length;
}
}
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.Back)
{
if (Text.Length == 5)
{
Text = Text.Replace(",", "");
SelectionLength = 1;
SelectionStart += Text.Length;
}
}
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
var txt = Text.Replace(",", "");
SetValue(ValueProperty, txt.Length==0?0:double.Parse(txt));
base.OnTextChanged(e);
}
private static bool IsTextAllowed(string text)
{
try
{
double.Parse(text);
return true;
}
catch (FormatException)
{
return false;
}
}