如何在WPF中创建具有依赖项属性的数字文本框自定义控件? [英] How to create numeric Textbox Custom Control with dependency Property in WPF?

查看:84
本文介绍了如何在WPF中创建具有依赖项属性的数字文本框自定义控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF中为具有依赖项属性的数字文本框创建一个自定义控件,在我的解决方案中,我添加了一个WPF应用程序和自定义控件(WPF),然后在公共类中创建了依赖项属性....

现在我不知道如何为文本框编写规则以及哪个事件为真?

另一个问题:我对数字文本框的规则是什么,该文本框必须被赋予数字.分隔.自定义文本框用于会计系统.

 公共静态只读DependencyProperty NumberTextbox;静态Numeric(){DefaultStyleKeyProperty.OverrideMetadata(typeof(Numeric),new FrameworkPropertyMetadata(typeof(Numeric))));FrameworkPropertyMetadata元数据=新的FrameworkPropertyMetadata("Enter Your Text",OnKeyDown);NumberTextbox = DependencyProperty.Register("Text",typeof(TextBox),typeof(FrameworkElement),元数据);}公共字符串NumberTXT{get {return(string)GetValue(NumberTextbox);}设置{SetValue(NumberTextbox,value);}} 

解决方案

我建议您在下面的示例代码中添加另一个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("###,###,###");}} 

endregion

 受保护的重写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

region Value property

    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("###,###,###"); }
    }

endregion

    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;
        }
    }

这篇关于如何在WPF中创建具有依赖项属性的数字文本框自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆