Normally, you do not want the UpdateSourceTrigger be PropertyChanged in the TextBox.Text binding, as this triggers a check and change notification every time a key is pressed.
If you do this only so that if the user presses Enter, he saves the value before processing the save command, then I suggest connecting to the PreviewKeyDown event and manually updating the source if the Enter key is pressed (I usually do this 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 , consider using formatting when displaying a value, but delete it while the user edits 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>
Rachel Nov 18 '11 at 16:32 2011-11-18 16:32
source share