首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >格式化后奇怪的文本框插入符号行为

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

Stack Overflow用户
提问于 2014-11-11 21:59:54
回答 1查看 385关注 0票数 5

我已经编写了简单的WPF自定义textbox控件来保存十进制值。以下是其代码的一个非常“轻量级”的版本,没有大部分方法和属性(但对于测试来说应该足够了):

代码语言:javascript
复制
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位。如果我键入不同的数字,插入符号会跳到文本的末尾。如果格式中没有小数位,则一切正常。我尝试过手动设置格式,但没有成功。下面是一个小插图:

​

EN

回答 1

Stack Overflow用户

发布于 2015-01-03 02:54:46

如果要将插入符号移到小数点之前,请尝试执行以下操作:

代码语言:javascript
复制
this.CaretIndex = string.Substring(0, this.Text.IndexOf(".")).Length;

  • 如果上面的代码将插入符号移动到小数点后,则将其更改为:

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

如果要将插入符号移动到框的开头:

代码语言:javascript
复制
this.CaretIndex = 0;

如果要将插入符号移动到框的末尾:

代码语言:javascript
复制
this.CaretIndex = this.Text.Length;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26866597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档