C# 转换器中WPF输入的国际化

C# 转换器中WPF输入的国际化,c#,wpf,internationalization,C#,Wpf,Internationalization,我正在为我的WPF应用程序编写一个转换器,它需要为一列显示可变的小数位数。用户可以使用组合键或表单按钮更改列的小数位数。唯一的区别是,如果精度增加,并且最后一个数字是0,则精度不会增加 因此,对于精度为3位小数的列,以下值123.123和123.100将显示为123.123和123.1 目前这对我来说很有效。当我尝试将其国际化时,问题就出现了。我已经阅读了很多关于国际化的文章和页面,但是似乎仍然无法正确地实现这一点。如果有人能看一下并指出我可能忽略的任何明显问题,我将不胜感激 正在观察的行为如下

我正在为我的WPF应用程序编写一个转换器,它需要为一列显示可变的小数位数。用户可以使用组合键或表单按钮更改列的小数位数。唯一的区别是,如果精度增加,并且最后一个数字是0,则精度不会增加

因此,对于精度为3位小数的列,以下值123.123和123.100将显示为123.123和123.1

目前这对我来说很有效。当我尝试将其国际化时,问题就出现了。我已经阅读了很多关于国际化的文章和页面,但是似乎仍然无法正确地实现这一点。如果有人能看一下并指出我可能忽略的任何明显问题,我将不胜感激

正在观察的行为如下所示。显示数据时,转换器似乎工作正常,并根据用户的区域设置正确格式化数字。当用户更新数据时,德国用户会遇到以下行为

输入显示的预期显示 1000 1.000 1.000 1.000 1 1.000 1,000 1.000 1 1234 1.234 1234

我的转换器代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Data;

namespace xxx
{
    [ValueConversion(typeof(object), typeof(string))]
    public class StripDecimalsConverter : IValueConverter
    {
        public static readonly Regex NumberRegex = new Regex("(?<=[A-Z])(\\d+)", RegexOptions.Compiled);

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return any integer values to 0 dp. Else apply the default decimal places to the value.
            string convertedformat = parameter.ToString().ToUpper().Contains("P") ? "{0:P0}" : "{0:N0}";

            string format = parameter.ToString();

            int converted;

            var convertedCulture = System.Globalization.CultureInfo.CurrentCulture;

            if (value == null) return string.Format(culture, format, string.Empty);
            else if (int.TryParse(value.ToString(), out converted))
                return string.Format(convertedCulture, convertedformat, converted);
            else
            {

                Double d;
                Double.TryParse(value.ToString(), out d);
                var x = (double)value;
                var strippedFormat = StrippedFormat(x, format, convertedCulture);

                return string.Format(convertedCulture, strippedFormat, x);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }



        private string StrippedFormat(double value, string format, System.Globalization.CultureInfo culture)
        {

            var scaleMatch = NumberRegex.Match(format);
            //What format value is being sent in by the formatter?
            int requiredprecision = scaleMatch != Match.Empty ? Int32.Parse(scaleMatch.Value) : 0;

            Double d = value;

            int index = d.ToString().IndexOf(culture.NumberFormat.NumberDecimalSeparator);
            //How many decimakl places are in the value to be formatted?
            var formatlength = d.ToString().Substring(index + 1).Length;
            string newformat = requiredprecision > formatlength
                            ? (format.Contains("P") ? string.Format("{{0:P{0}}}", formatlength) : string.Format("{{0:N{0}}}", formatlength))
                            : (format.Contains("P")
                                   ? string.Format("{{0:P{0}}}", requiredprecision)
                                   : string.Format("{{0:N{0}}}", requiredprecision));

            return newformat;
        }
    }
}
My App.xaml还包含以下行:

Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
任何指向正确方向的指针都将受到极大的赞赏

Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;