Skip to content

Commit

Permalink
Fix regression bug with selection of first find character
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandreu committed Jul 7, 2019
1 parent 15fac3f commit 6a65b89
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions OfficeRibbonXEditor/Controls/EditableComboBox.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace OfficeRibbonXEditor.Controls
{
Expand All @@ -18,22 +20,55 @@ public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.TextBox.TextChanged += this.OnTextChanged;
this.AddHandler(PreviewMouseLeftButtonDownEvent,
new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true);
this.AddHandler(GotKeyboardFocusEvent,
new RoutedEventHandler(SelectAllText), true);
this.AddHandler(MouseDoubleClickEvent,
new RoutedEventHandler(SelectAllText), true);
}

private static void SelectivelyIgnoreMouseButton(object sender,
MouseButtonEventArgs e)
{
// Find the TextBox
DependencyObject parent = e.OriginalSource as UIElement;
while (parent != null && !(parent is TextBox))
parent = VisualTreeHelper.GetParent(parent);

if (parent != null)
{
var textBox = (TextBox)parent;
if (!textBox.IsKeyboardFocusWithin)
{
// If the text box is not yet focussed, give it the focus and
// stop further processing of this click event.
textBox.Focus();
e.Handled = true;
}
}
}

private static void SelectAllText(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is TextBox textBox)
{
textBox.SelectAll();
}
}

protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);

this.TextBox?.Focus();
this.TextBox?.SelectAll();
}

private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var txt = (TextBox) sender;
txt.TextChanged -= this.OnTextChanged;
txt.Focus();
txt.SelectAll();
}
}
}

0 comments on commit 6a65b89

Please sign in to comment.