6

I have got how to SelectAll text when clicked on a TextBox; I want to do the same for an editable combobox - din find anything. My code for TextBox is

private void OnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
    txtBox.SelectAll();
    txtBox.Focus();
    e.Handled = true;
}

How can the same can be done for the Editable Combobox ?

Update Code for Combox that gives me the output that I want:

private void cboMouseDown(object sender, MouseButtonEventArgs e)
        {
            var textBox = (cbo.Template.FindName("PART_EditableTextBox", cbo) as TextBox);
            if (textBox != null)
            {
                textBox.SelectAll();
                cbo.Focus();
                e.Handled = true;
            }
        }

But now the dropdown of the combobox doesn't work, any suggestion ?

Update-2: Instead of PreviewMouseDown - I have tried PreviewMouseUp and now the dropdown does appear; but when once clicked on the box and then tried to open the dropdown - the window becomes frozen. However, I have made a work around that I have put in my answer bellow. I would really appreciate your comments though if it is a right and safe solution I can go with.

marifrahman
  • 681
  • 2
  • 13
  • 31
  • Why don't you use the `Focus` event (on both controls)? Mouse events are not working in all cases (Keyboard navigation, touch, ...). – Christoph Fink Jun 12 '15 at 05:43
  • On GotFocus the text is by default selected. My requirement is after typing some characters if the user clicks on what he typed - the entire text gets selected so that he can start over. The default behavior though is when you click first time the entire text gets selected; but on next click the cursor goes to the point where you clicked and the text remains un selected. – marifrahman Jun 12 '15 at 06:04
  • 1
    **Just Check out these links-** [Select ComboBox Text][1] [How to add a focus to an editable ComboBox in WPF][2] [1]: http://stackoverflow.com/q/19728650/2025489 [2]: http://stackoverflow.com/a/2964222/2025489 – LogicalDesk Jun 12 '15 at 06:05
  • The links are useful and I do get what I want - but now the dropdown doesn't open. – marifrahman Jun 12 '15 at 06:28

3 Answers3

6

Use GotFocus event and select text like this

    var comboTextBoxChild = comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;

    comboTextBoxChild .SelectAll();

Here combobox is your Editable Combobox name

Nitin
  • 18,344
  • 2
  • 36
  • 53
  • If I do this on PreviewMouseDown of the Combobox I get what I want - but the issue is the dropdown doesn't open. – marifrahman Jun 12 '15 at 06:27
5

A possible solution I have got and its working for me - need some suggestion though if it is ok or not; I am using PreviewMouseUp event of the ComboBox:

private void cboMouseUp(object sender, MouseButtonEventArgs e)
        {
            var textBox = (cbo.Template.FindName("PART_EditableTextBox", cbo) as TextBox);
            if (textBox != null && !cbo.IsDropDownOpen)
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(()=>{
                    textBox.SelectAll();
                    textBox.Focus();
                    //e.Handled = true;
                }));
            }
marifrahman
  • 681
  • 2
  • 13
  • 31
1

I am a little late to party, but I had same problem recently and after testing several solutions I came up with my own (I created custom control for this purpose):

public class ComboBoxAutoSelect : ComboBox
{
    private TextBoxBase textBox;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        textBox = GetTemplateChild("PART_EditableTextBox") as TextBoxBase;
    }

    protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
    {
        // if event is called from ComboBox itself and not from any of underlying controls
        // and if textBox is defined in control template
        if (e.OriginalSource == e.Source && textBox != null)
        {
            textBox.Focus();
            textBox.SelectAll();
            e.Handled = true;
        }
        else
        {
            base.OnPreviewGotKeyboardFocus(e);
        }
    }
}

you can do the same with events but you will need to search for "PART_EditableTextBox" every time and here we do it only once per template change

Adassko
  • 5,201
  • 20
  • 37