Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 570273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:23:02+00:00 2026-05-13T13:23:02+00:00

I have a WPF ComboBox which has its IsEditable property bound to a view

  • 0

I have a WPF ComboBox which has its IsEditable property bound to a view model which can switch it on and off. When it is switched on, I want to give focus to the ComboBox and select all the text in the edit TextBox.

I can’t see the best to accomplish this. Should I replace the ControlTemplate, subclass the ComboBox base class, and provide the needed properties, use Attached Properties, or some other approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:23:02+00:00Added an answer on May 13, 2026 at 1:23 pm

    I have a solution for you.

    I’m using a combination of Model-View-ViewModel and Attached Property approach.

    Firstly, you need to know the Messaging system in MVVM for this to work, as well as know your Commands. So starting with Attached Properties, we begin to set an IsFocused event to the combo box we would like to have focused and all text selected.

      #region Select On Focus
    
      public static bool GetSelectWhenFocused(DependencyObject obj)
      {
        return (bool)obj.GetValue(SelectWhenFocusedProperty);
      }
    
      public static void SetSelectWhenFocused(DependencyObject obj, bool value)
      {
        obj.SetValue(SelectWhenFocusedProperty, value);
      }
    
      // Using a DependencyProperty as the backing store for SelectWhenFocused.  This enables animation, styling, binding, etc...
      public static read-only DependencyProperty SelectWhenFocusedProperty =
          DependencyProperty.RegisterAttached("SelectWhenFocused", typeof(bool), typeof(EditableComboBox), new UIPropertyMetadata(OnSelectOnFocusedChanged));
    
      public static void OnSelectOnFocusedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
      {
        bool SetProperty = (bool)args.NewValue;     
    
        var comboBox = obj as ComboBox;
        if (comboBox == null) return;
    
        if (SetProperty)
        {
          comboBox.GotFocus += GotFocused;
          Messenger.Default.Register<ComboBox>(comboBox, Focus);
        }
        else
        {
          comboBox.GotFocus -= GotFocused;
          Messenger.Default.Unregister<ComboBox>(comboBox, Focus);
        }
      }
    
      public static void GotFocused(object sender, RoutedEventArgs e)
      {
        var comboBox = sender as ComboBox;
        if(comboBox == null) return;
    
        var textBox = comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;
        if (textBox == null) return;
    
        textBox.SelectAll();
      }
    
      public static void Focus(ComboBox comboBox)
      {
        if(comboBox == null) return;
        comboBox.Focus();
      }
      #endregion
    

    What this code shows is when we set the Attached Property SelectWhenFocused to true, it will register to listen for the GotFocused Event and select all the text inside.

    To use is simple:

    <ComboBox
      IsEditable="True"
      ComboBoxHelper:EditableComboBox.SelectWhenFocused="True"
      x:Name="EditBox" />
    

    Now we need a button that will set the focus on the ComboBox when clicked on.

    <Button
      Command="{Binding Focus}"
      CommandParameter="{Binding ElementName=EditBox}"
      Grid.Column="1" >Focus</Button>
    

    Notice how the CommandParameter is binding to the ComboBox by its name EditBox. This is so when the command executes, only this ComboBox gets focused and all text selected.

    In my ViewModel, I have the Focus command declared as following:

    public SimpleCommand Focus { get; set; }
    public WindowVM()
    {
      Focus = new SimpleCommand {ExecuteDelegate = x => Broadcast(x as ComboBox)};
    }
    

    This is a tested and proven technique that works for me. I hope it’s not an overkill solution to your problem. Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ComboBox in WPF which is databound, and has data template which
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,
I have a WPF Window which has a among other controls hosts a Frame.
I have a comboBox that has a datatrigger that set its SelectedIndex based on
I have a WPF window for editing database information, which is represented using an
I have a WPF control, that has a list of Investors, and in the
I have a WPF app which snaps to screen edges (I just set the
I have a WPF ListView that is bound to a BindingList<T>. The binding works
I have a requirement to change a very small part of the WPF ComboBox's
In my WPF app I have a data import function which imports data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.