具有十进制格式的 WPF 自定义文本框 [英] WPF Custom TextBox with Decimal Formatting

查看:18
本文介绍了具有十进制格式的 WPF 自定义文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WPF 新手.我有一个要求,我需要开发一个自定义文本框控件,它应该支持以下功能:

I am new to WPF. I have a requirement that I need to develop a custom textbox control which should support the functionality like:

  1. 应该只接受十进制值.

  1. Should accept only decimal values.

在通过代码或由用户分配值时,应四舍五入到小数点后 3 位.

Should round off to 3 decimal places when assigned a value through code or by the user.

应该在焦点上显示完整的值(不带格式).

Should show the full value(without formatting) on focus.

例如:

如果将 2.21457 分配给文本框(通过代码或用户),它应该显示 2.215.当用户单击它进行编辑时,它必须显示完整的值 2.21457.用户将值编辑为 5.42235 并制表符后,它应该再次四舍五入为 5.422.

If 2.21457 is assigned to textbox(by code or by user), it should display 2.215. When user clicks in it to edit it, it must show the full value 2.21457. After the user edits the value to 5.42235 and tabs out, it should again round off to 5.422.

试过了,没有成功.所以需要一些帮助.提前感谢您的帮助.

Tried it without success. So need some help on it. Thanks in advance for the help.

谢谢

推荐答案

我编写了一个自定义控件,它具有名为 ActualText 的依赖属性.将您的值绑定到 ActualText 属性中,并在 gotfocus 和 lostfocus 事件期间操纵文本框的 Text 属性.还在 PreviewTextInput 事件中验证十进制数.参考下面的代码.

I have written a custom control which will have dependency property called ActualText. Bind your value into that ActualText property and manipulated the Text property of the textbox during the gotfocus and lostfocus event. Also validated for decimal number in the PreviewTextInput event. refer the below code.

 class TextBoxEx:TextBox
{
    public string ActualText
    {
        get { return (string)GetValue(ActualTextProperty); }
        set { SetValue(ActualTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ActualText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ActualTextProperty =
        DependencyProperty.Register("ActualText", typeof(string), typeof(TextBoxEx), new PropertyMetadata(string.Empty, OnActualTextChanged));

    private static void OnActualTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBox tx = d as TextBox;
        tx.Text = (string)e.NewValue;
        string str = tx.Text;            
        double dbl = Convert.ToDouble(str);
        str = string.Format("{0:0.###}", dbl);
        tx.Text = str;
    }

    public TextBoxEx()
    {
        this.GotFocus += TextBoxEx_GotFocus;
        this.LostFocus += TextBoxEx_LostFocus;
        this.PreviewTextInput += TextBoxEx_PreviewTextInput;
    }

    void TextBoxEx_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
    {
        decimal d;
        if(!decimal.TryParse(e.Text,out d))
        {
            e.Handled = true;
        }
    }        

    void TextBoxEx_LostFocus(object sender, System.Windows.RoutedEventArgs e)
    {
        ConvertText();
    }

    void TextBoxEx_GotFocus(object sender, System.Windows.RoutedEventArgs e)
    {
        this.Text = ActualText;
    }

    private void ConvertText()
    {
        string str = this.Text;
        ActualText = str;
        double dbl = Convert.ToDouble(str);
        str = string.Format("{0:0.###}", dbl);
        this.Text = str;
    }
}

这篇关于具有十进制格式的 WPF 自定义文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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