格式化后奇怪的文本框插入符号行为

时间:2014-11-11 13:59:54

标签: c# wpf textbox formatting wpf-controls

我编写了简单的WPF自定义文本框控件来保存小数值。这是一个非常“轻”的代码版本,没有大多数方法和属性(但它应该足以进行测试):

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace CustomControls
{
    public class NumericBox : TextBox
    {
        public static readonly DependencyProperty ValueProperty;
        public static readonly DependencyProperty DecimalCountProperty;

        static NumericBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericBox), new FrameworkPropertyMetadata(typeof(NumericBox)));
            ValueProperty = DependencyProperty.Register("Value", typeof (decimal?), typeof (NumericBox),
                new FrameworkPropertyMetadata(0m, OnValueChanged));
            DecimalCountProperty = DependencyProperty.Register("DecimalCount", typeof(int), typeof(NumericBox),
                new FrameworkPropertyMetadata(2, OnDecimalCountChanged));

        }
        public int DecimalCount
        {
            get { return (int)GetValue(DecimalCountProperty); }
            set { SetValue(DecimalCountProperty, value); }
        }
        public decimal? Value
        {
            get { return (decimal?)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        private static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
        }
        private static void OnDecimalCountChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            BindingOperations.ClearAllBindings(this);
            var textBinding = new Binding("Value")
            {
                Converter = new TextToNumericBoxValueConverter(),
                Mode = BindingMode.TwoWay,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                RelativeSource = new RelativeSource(RelativeSourceMode.Self),
                ConverterParameter = DecimalCount
            };
            SetBinding(TextProperty, textBinding);
        }
    }

    public class TextToNumericBoxValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var d = value as decimal?;
            if (d == null) return "";
            var p = parameter is int ? (int)parameter : 0;
            var nf = culture.NumberFormat.Clone() as NumberFormatInfo;
            if (nf == null) return "";
            nf.NumberDecimalDigits = p;
            return d.Value.ToString("N", nf);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            decimal d;
            var s = value as string;
            if (s == null)
                return null;
            if (decimal.TryParse(s, out d))
                return d;
            return null;
        }
    }
}

现在奇怪的事情开始了。如果我连续输入几个相同的数字,一切都可以,直到输入第10个数字。然后插入符号跳到第5个位置。这是2位小数的情况。例如,在3个十进制数字的情况下,插入符号在第11次击键后跳到第6个位置。 如果我输入不同的数字,则插入符号会跳到文本的末尾。 如果格式中没有十进制数字 - 一切正常。 我试图手动设置格式 - 没有成功。 这是一个小例子: caret problem

1 个答案:

答案 0 :(得分:0)

如果您想在十进制之前移动插入符号,请尝试:

this.CaretIndex = string.Substring(0, this.Text.IndexOf(".")).Length;
  • 如果上述内容移动小数点后的插入符号,请将其更改为:

    this.CaretIndex = string.Substring(0, this.Text.IndexOf(".") - 1).Length;

如果您想将插入符号移到框的开头:

this.CaretIndex = 0;

如果您想将插入符号移到框的末尾:

this.CaretIndex = this.Text.Length;