Question

I have a DevExpress (I will refer to it as "DX" for this question) control, the TextEdit.

Here a sample of what I have

<Grid>
    <dxe:TextEdit x:Name="te" MaskType="Numeric" Mask="###,##0.###"
                  MaskUseAsDisplayFormat="True" Validate="te_Validate"
                  Text="{Binding myValeur, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

The Mask Mask="###,##0.###" is normal, I'm limited to 9 numbers with 3 decimals.

I do this in my codebehind

decimal myDecimalValue = 0.0m;
private void te_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
{
    Thread Thread_te_Validate = new Thread(delegate()
    {
        this.Dispatcher.BeginInvoke(new Action(delegate()
        {
            if (!String.IsNullOrEmpty(te.Text))
                myDecimalValue = Convert.ToDecimal(te.Text);
        }));
    });
    Thread_te_Validate.IsBackground = true;
    Thread_te_Validate.Start();
}

This code works with a strange problem. If I write 100 in my TextEdit, the value is correctly converted. If I write 100.123 (so I put some decimals), the application crashes with the error FormatException was unhandled by user code (french traduction).

In details, I have {"The format of the input string is incorrect."}

The value of the TextEdit.Text at the crash is good 100.123 but the decimal only store 100 .

I'm french and use french culture so the 'comma' is the decimal separator for the UI but the variables are stored correctly with a 'point' in decimal separator.

Edit : I test this code and the same error is going. So I think the problem is the conversion to decimal. The string get the value with decimals but the conversion bug.

if (!String.IsNullOrEmpty(te.Text))
{ 
    string test1 = te.Text;
    myDecimalValue = Convert.ToDecimal(test1);
}

Edit 2 : This code don't woks too.

if (!String.IsNullOrEmpty(te.Text))
{ 
    string test1 = te.Text;
    myDecimalValue = decimal.Parse(test1);
}

Edit 3 : I also tried forcing culture during the conversion. With this code, I can not insert a decimal in my TextEdit, only integers.

myDecimalValue = Convert.ToDecimal(te.Text, CultureInfo.InvariantCulture);

My specific culture send the FormatException error.

myDecimalValue = Convert.ToDecimal(te.Text, CultureInfo.CreateSpecificCulture("fr-FR"));

My neutral culture send the FormatException error too.

myDecimalValue = Convert.ToDecimal(te.Text, CultureInfo.CreateSpecificCulture("fr"));

Solution working for me : This code works for me. I define my Langage at 'fr' in App.xaml but apparently, it's not suffisent. So the problem is really the Culture and of course, the decimal character.

For me, I need to replace the 'dot' with a 'comma' because the culture in DevExpress show me the good culture but store in US culture.

if (!String.IsNullOrEmpty(myDXTextEdit.Text))
    myDecimalValue = Convert.ToDecimal(myDXTextEdit.Text.Replace(".", ","));
Was it helpful?

Solution

By default, WPF uses en-US as the culture, regardless of the system settings. I believe you have not override this behavior, and moreover you've included the 'dot' as a decimal separator in the TextEdit.Mask. So far so good. That because you have received 100.23(with 'dot' decimal separator) as a value. However the decimal conversion uses your current culture (which is French) as IFormatProvider thus you can not convert the text into decimal value using this culture (that is the reason of the exception).

To avoid the exception, please specify the correct culture for conversion (you can use the InvariantCulture instead of en-US):

myDecimalValue = Convert.ToDecimal(te.Text, CultureInfo.InvariantCulture);

OTHER TIPS

After much research, it turned out that there were two problems.

First, the Binding of TextEdit were made on variables modified by TextEdit which created strange loops and bug to enter decimal.

Second, even if my application is defined in French culture, it must still force String.Replace (".", "") In order to properly perform the conversions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top