Search code examples
wpfmvvm

WPF: Textbox and Binding to Double not able to type . on it


I have a text box like

<TextBox Text="{Binding TransactionDetails.TransactionAmount, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.ColumnSpan="2" Grid.Row="5" 
x:Name="TextBoxAmount"/>

And I have taken "TransactionAmount" as Double. Its working well on integer value but when I am typing some Floating point value like 100.456 I am not able to type '.'


Solution

  • The best solution I got by using StringFormat like

    <TextBox Text="{Binding TransactionDetails.TransactionAmount, Mode=TwoWay, 
    UpdateSourceTrigger=PropertyChanged,StringFormat=N2}" Grid.Column="3" 
    Grid.ColumnSpan="2" Grid.Row="5" x:Name="TextBoxAmount" />
    

    Also we can go for custom string format as per requirements