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(".", ","));