'WPF DataGrid using wrong NumberDecimalSeparator

currently I am facing the following problem. In my application (which is a plugin for AutoCAD/BricksCAD), i am using a DataGrid with a custom Column, that ensures the input to be in a float-format. The code for the column looks something like that:

public class DataGridFloatColumn : DataGridTextColumn
{
    protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
    {
        TextBox edit = editingElement as TextBox;
        edit.PreviewTextInput += OnPreviewTextInput;
        return base.PrepareCellForEdit(editingElement, editingEventArgs);
    }

    void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        string txt = ((TextBox)sender).Text + e.Text;
        float _;
        e.Handled = !float.TryParse(txt, out _);
    }
}

This part works as expected. I am only allowed to input float numbers, separated by a comma. The problem however is, that the DataGrid expects a dot as decimal separator. Due to this, initial values are represented with a dot, and If I type something with a comma, it simply gets removed when hitting enter... for example 123,4 --> 1234

What I have already tried:

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");

But when printing Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator, i am getting a comma...

Any suggestions? Thank you ind advance!



Solution 1:[1]

I dont know if it covers your issue, but if I want culture-specific number/datetime formatting in XAML, I put this "hack" into the constructor of my "App.xaml.cs"

public App()
{
    FrameworkElement.LanguageProperty.OverrideMetadata(                                                                     
                   typeof(FrameworkElement),
                   new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));
}

Thread.CurrentThread.CurrentCulture = myCulture somehow does not do the trick.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lidqy