3

Using a TextBox in WPF I have the problem that if I use ',' instead of '.' every time I try to get the value, the text inside the TextBox is transformed with the same number without comma..

How can I disable this automatic transformation?

<TextBox 
    x:Name="XValue" 
    Text="{Binding XInitValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    Width="80" VerticalAlignment="Center" 
    TextChanged="XValue_TextChanged" 
</TextBox>

private void XValue_TextChanged(object sender, TextChangedEventArgs e)
{
    double a = XInitValue;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    Please take a look at this answer: https://stackoverflow.com/questions/9160059/set-up-dot-instead-of-comma-in-numeric-values – CCamilo Jul 03 '17 at 10:31

2 Answers2

1

It really works! Thanks!

I have change it a bit to be more general, not use the CurrentCulture in the converter itself and return error if the entered value is ended with decimal separator. Without last part I could not enter the decimal separator at all if the UpdateSourceTrigger=PropertyChanged.

public class DecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return null;
        }
        if (value is string stringValue && targetType == typeof(decimal))
        {
            var decSeparator = culture.NumberFormat.NumberDecimalSeparator;
            var normString = decSeparator == "."
                ? stringValue.Replace(",", ".")
                : stringValue.Replace(".", ",");
            if (!normString.EndsWith(decSeparator) && decimal.TryParse(normString, out var decResult))
            {
                return decResult;
            }
        }
        return DependencyProperty.UnsetValue;
    }
}

Certainly the CurrentCulture should be set in App.xml.cs or somewhere else on starting the application:

    var culture = CultureInfo.GetCultureInfo("de-DE");
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;
    FrameworkElement.LanguageProperty.OverrideMetadata(
        typeof(FrameworkElement),
        new FrameworkPropertyMetadata(
            XmlLanguage.GetLanguage(culture.IetfLanguageTag)));
0

I solved it using an converter

public class DecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ".")
                return value.ToString().Replace(",", ".");
            else
                return value.ToString().Replace(".", ",");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ".")
                return value.ToString().Replace(".", ",");
            else
                return value.ToString().Replace(",", ".");
        }

    }