Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF editable ComboBox SelectAll when clicked

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.

like image 537
marifrahman Avatar asked Jun 12 '15 05:06

marifrahman


3 Answers

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

like image 178
Nitin Avatar answered Nov 02 '22 02:11

Nitin


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;
                }));
            }
like image 5
marifrahman Avatar answered Nov 02 '22 03:11

marifrahman


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

like image 1
Adassko Avatar answered Nov 02 '22 02:11

Adassko