2

I have a WPF application with a DataGrid which is bound to an ObservableCollection. The collection type is a class which contains one or more properties of type double for decimal values. In the XAML i have defined DataGrid-Columns like that:

<DataGrid.Columns>
    <DataGridTextColumn x:Name="col_LowerBound"
                        Binding="{Binding LowerBound, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                        Header="Lower bound"/>
</DataGrid.Columns>

As You can see, i'm using the UpdateSourceTrigger LostFocus instead of PropertyChanged. In case of PropertyChanged the input is checked all at once and a character like "." would cause an inplausibility. By using LostFocus i'm able to input decimals with dot.

Now i want to input comma too and replace it with dot. Can i make that replacement while the KeyDown- or PreviewKeyDown-event and how? I don't know how to do replace values while KeyDown and a similar case with TextBox is no great help.

3
  • 1
    Possible duplicate of Can you replace characters in a textbox as you type?. You indeed have the right idea, this question and its answers should give you all the insight you need to accomplish this. Commented Jan 24, 2017 at 15:15
  • Hello Kilazur. Thank You for the hint, but unfortunately the TextBox is not the same as the DataGrid and it's cells. Meanwhile i have found a better solution using a converter which You can watch below. Commented Jan 25, 2017 at 9:35
  • You'll notice one of the answer talks about using a converter :p Commented Jan 25, 2017 at 16:49

1 Answer 1

6

I have found a veritable and simple solution which uses a converter class.

First we need a converter class which replaces any comma in the input by a dot. The joke is that we have to make the same replacement in the method Convert as well as in the method ConvertBack. :D

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

namespace P16_StepFunctions
{
    /// <summary>
    /// Class for replacing comma by dot in input of decimal fields.
    /// </summary>
    public class DecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString().Replace(",", ".");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString().Replace(",", ".");
        }

    }
}

Then we add in the XAML of the window a resource to the converter class like that:

<Window.Resources>
    <local:DecimalConverter x:Key="decimalconverter"/>
</Window.Resources>

And finally we add a Converter-attribute to the binding of all datagrid columns which shall contain decimal values. This attribute gets the converter as staticresource.

<DataGridTextColumn x:Name="col_LowerBound"
                    Binding="{Binding LowerBound, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource decimalconverter}}"
                    Header="Lower bound"/>

And that's all. While typing in one of these datagrid cells, commas are replaced by dots after the cursor has left the cell. In case of wrong input like "1,1,1" the cell gets a red border and an error sign "!" is shown automatically at the left side of the datagrid row. Then the user can edit his mistake.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.