diff --git a/src/client/spark/view/Extensions/TextElement/TextBoxBehavior.cs b/src/client/spark/view/Extensions/TextElement/TextBoxBehavior.cs index 1294e3678..0da35d793 100644 --- a/src/client/spark/view/Extensions/TextElement/TextBoxBehavior.cs +++ b/src/client/spark/view/Extensions/TextElement/TextBoxBehavior.cs @@ -32,7 +32,7 @@ namespace mbts.ui { #region TraverseOnEnter - public static readonly DependencyProperty TraverseOnEnterProperty = DependencyProperty.RegisterAttached("TraverseOnEnter", typeof(bool), typeof(TextBoxBehavior), new PropertyMetadata(false, OnTraverseOnEnterChanged)); + public static readonly DependencyProperty TraverseOnEnterProperty = DependencyProperty.RegisterAttached("TraverseOnEnter", typeof(bool), typeof(TextBoxBehavior), new PropertyMetadata(false, OnTraverseOnEnterChanged)); private static void OnTraverseOnEnterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var text_box = (TextBox)d; if ((bool)e.NewValue) text_box.PreviewKeyDown += commit_on_enter_PreviewKeyDownHandler; @@ -84,9 +84,16 @@ namespace mbts.ui { static void tbx_keydown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { - var binding = BindingOperations.GetBindingExpression(sender as DependencyObject, TextBox.TextProperty); - binding.UpdateTarget(); - binding.UpdateSource(); + abort_edit(sender as DependencyObject); + } + } + + // Copy the binding value back and forth to generate a set to the source property with the original value + private static void abort_edit(DependencyObject sender) { + if (sender != null) { + var binding = BindingOperations.GetBindingExpression(sender, TextBox.TextProperty); + binding?.UpdateTarget(); + binding?.UpdateSource(); } } @@ -95,6 +102,29 @@ namespace mbts.ui { #endregion + #region TryCancelOnFocusLost + public static readonly DependencyProperty TryCancelOnFocusLostProperty = + DependencyProperty.RegisterAttached("TryCancelOnFocusLost", typeof(bool), typeof(TextBoxBehavior), new PropertyMetadata(false, cancel_on_focus_lost_changed)); + + private static void cancel_on_focus_lost_changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { + var tb = (TextBox)d; + if ((bool)e.NewValue) { + tb.LostFocus += tb_lostfocus; + } + else { + tb.LostFocus += tb_lostfocus; + } + } + + static void tb_lostfocus(object sender, EventArgs e) { + abort_edit(sender as DependencyObject); + } + + public static bool GetTryCancelOnFocusLost(DependencyObject obj) => (bool)obj.GetValue(TryCancelOnFocusLostProperty); + public static void SetTryCancelOnFocusLost(DependencyObject obj, bool value) => obj.SetValue(TryCancelOnFocusLostProperty, value); + + #endregion + #region ReleaseOnEscape public static readonly DependencyProperty ReleaseOnEscapeProperty = DependencyProperty.RegisterAttached( @@ -108,7 +138,8 @@ namespace mbts.ui { tb.ReleaseMouseCapture(); Keyboard.ClearFocus(); })); - } else { + } + else { tb.ClearKeyBindings(Key.Escape, ModifierKeys.None); tb.ClearCommandBindings(ReleaseCaptureCommand); } @@ -209,17 +240,18 @@ namespace mbts.ui { "IncrementDecrementOnMouseClick", typeof(bool), typeof(TextBoxBehavior), new PropertyMetadata(false, IncrementDecrementOnMouseClickPropertyChanged) ); - public static bool GetIncrementDecrementOnMouseClick(TextBox element) { return (bool)element.GetValue(IncrementDecrementOnMouseClickProperty); } + public static bool GetIncrementDecrementOnMouseClick(TextBox element) { return (bool)element.GetValue(IncrementDecrementOnMouseClickProperty); } public static void SetIncrementDecrementOnMouseClick(TextBox element, bool value) { element.SetValue(IncrementDecrementOnMouseClickProperty, value); } private static void IncrementDecrementOnMouseClickPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var text_box = d as TextBox; - if(text_box != null) { + if (text_box != null) { if ((bool)e.NewValue) { text_box.PreviewMouseLeftButtonDown += OnMouseLeftButtonDownDecrementValue; text_box.PreviewMouseRightButtonDown += OnMouseRightButtonDownIncrementValue; - } else { + } + else { text_box.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDownDecrementValue; text_box.PreviewMouseRightButtonDown -= OnMouseRightButtonDownIncrementValue; } @@ -230,7 +262,8 @@ namespace mbts.ui { var text_box = sender as TextBox; if (text_box == null || String.IsNullOrWhiteSpace(text_box.Text)) { return; - } else { + } + else { if (uint.TryParse(text_box.Text, out uint val)) { if (val < uint.MaxValue) { val++; @@ -255,5 +288,6 @@ namespace mbts.ui { } } #endregion + } } \ No newline at end of file diff --git a/src/client/spark/view/Shell/TabDocument.xaml b/src/client/spark/view/Shell/TabDocument.xaml index d88b0413f..85a306459 100644 --- a/src/client/spark/view/Shell/TabDocument.xaml +++ b/src/client/spark/view/Shell/TabDocument.xaml @@ -26,6 +26,7 @@ ui:TextBoxBehavior.FocusOnLoaded="True" ui:TextBoxBehavior.SelectAllOnFocus="True" ui:TextBoxBehavior.CancelOnEscape="True" + ui:TextBoxBehavior.TryCancelOnFocusLost="True" Width="150" HorizontalAlignment="Left" VerticalAlignment="Stretch" Grid.Column="0" Margin="0,0,4,0" />