user3692104 user3692104 - 5 months ago 29
C# Question

TextBox with TwoWay DataBinding (double) does not allow decimal separator

I have the following textbox:

<TextBox x:Name="VoorzieningBerging"
HorizontalAlignment="Right" TextChanged="SimulatieWijziging"
Margin="0,0,45,0" Width="60" Height="25">
<Binding Path="VoorzieningParameters[0].Double"
ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"
FallbackValue="0" Mode="OneWayToSource">

And in the viewModel:

VoorzieningParameters = new ObservableCollection<Parameter>();
VoorzieningParameters.Add(new Parameter() { Naam = "berging" });

Where Parameter is a Class with a description (Naam) and a double for value.

public class Parameter
public Parameter()


public string Naam { get; set; }
public double? Double { get; set; }

This is working fine, I enter data in the textbox and when I hit the save button I loop through all the "Parameters" in my ObservableCollection and write them to XML. However now I want to make a load button but instead of writing the values into the Textboxes I'd like to just set the Parameters in the viewmodel and use a TwoWay binding instead. However when I change the binding mode to TwoWay the Textbox no longer accepts a decimal separator (, or .). When I try to input 7.0 it just sais 70.

Ive tried using string format on the Textbox, it then allows a seperator but when I try to input something like 7.0 it becomes 7.0.0

How do I solve this problem?


When you set binding to double it automatically calls .ToString() so there is no need to use Converter to display its value in TextBox.Text. Unfortunately, it does not allow to put '.' or ',' as you described. I have couple solutions

  1. Like you said you can use StringFormat as follows, StringFormat={}{0:0.0}}, but then you cannot write 7.0 but only write 7 or change number after dot.
  2. Second approach is to change type of your property from Double to string. Then inputting ',' is allowed but you will have to cast it to double whenever you wish to use it.
  3. I ran into already available solution, take a look here