Binding to a double with StringFormat on a TextBox Binding to a double with StringFormat on a TextBox wpf wpf

Binding to a double with StringFormat on a TextBox


Usually you don't want UpdateSourceTrigger to be PropertyChanged on a TextBox.Text binding because this triggers the Validation and Change notification every time a key is pressed.

If you are doing this only so that if the user hits Enter it will save the value before processing the save command, then I'd suggest hooking into the PreviewKeyDown event and manually updating the source if the key pressed was Enter (Usually I make this an AttachedProperty)

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e){    if (e.Key == Key.Enter)    {        var obj = sender as UIElement;        BindingExpression textBinding = BindingOperations.GetBindingExpression(            obj, TextBox.TextProperty);        if (textBinding != null)            textBinding.UpdateSource();    }}

But with that being said, if you still wanted to use UpdateSourceTrigger=PropertyChanged, then consider using the formatting when displaying the value, but remove it while the user is editing it.

<TextBox>    <TextBox.Style>        <Style TargetType="{x:Type TextBox}">            <Setter Property="Text" Value="{Binding Path=MyDoubleValue, StringFormat=N2}" />            <Style.Triggers>                <Trigger Property="IsFocused" Value="True">                    <Setter Property="Text" Value="{Binding Path=MyDoubleValue, UpdateSourceTrigger=PropertyChanged}" />                </Trigger>            </Style.Triggers>        </Style>    </TextBox.Style></TextBox>