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

  • SEARCH
  • Home
  • 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 1013345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:05:11+00:00 2026-05-16T10:05:11+00:00

Question: Can anyone please provide a full code example that shows how one does

  • 0

Question: Can anyone please provide a full code example that shows how one does programmatically change the SelectedItem of a data-bound WPF ComboBox without using MyComboBox.SelectedIndex?

Code sample: Here is what I currently have.

XAML:

<Window x:Class="Wpf.ComboBoxDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ComboBox Name="MyComboBox" DisplayMemberPath="LastName" SelectedIndex="0"/>
    </Grid>
</Window>

Code-behind:

using System.Collections.ObjectModel;
using System.Windows;

namespace Wpf.ComboBoxDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ObservableCollection<Person> myPersonList = new ObservableCollection<Person>();

            Person personJobs = new Person("Steve", "Jobs");
            Person personGates = new Person("Bill", "Gates");

            myPersonList.Add(personJobs);
            myPersonList.Add(personGates);

            MyComboBox.ItemsSource = myPersonList;

            // How do I programmatically select the second Person, i.e. "Gates"?
            // The best pratice must be to somehow to set something like IsCurrentlySelected on the model, so the view update automatically. But how?
            MyComboBox.SelectedIndex = 1; // This works, but is there no way without using the index?

        }

        private class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }

            public Person(string firstName, string lastName)
            {
                FirstName = firstName;
                LastName = lastName;
            }
        }
    }
}

Similar questions: I have of course searched the Internet first, but found nothing that helped me.

  • Changing the SelectedItem of a enum-bound combobox inside ViewModel (MSDN)
  • Programmatically set ComboBox SelectedItem in WPF (3.5sp1) (Stack Overflow)
  • 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-16T10:05:12+00:00Added an answer on May 16, 2026 at 10:05 am

    At the top of my head (I might be wrong), make the window implement INotifyPropertyChanged and add the event:

    namespace Wpf.ComboBoxDemo
    {
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public MainWindow()
            {
    

    Then add a property for the currently selected item which notifies on changes:

            private Person _selected;
            public Person MySelected
            {
                get { return _selected; }
                set
                {
                    if (value != _selected)
                    {
                        _selected = value;            
                        if (PropertyChanged != null)
                        {
                            PropertyChanged(this,
                                new PropertyChangedEventArgs("MySelected"));
                        }
                    }
                }
            }
    

    Now bind the combobox (the binding could be more advanced here using FindAncestor but sometimes to keep things simple I put the datacontext in code behind):

    XAML:

            <ComboBox
                Name="MyComboBox"
                DisplayMemberPath="LastName"
                SelectedItem="{Binding MySelected}" />
    

    Code behind:

        public MainWindow()
        {
            InitializeComponent();
            // ...
    
            // this will cause the "MySelected" binding to target the correct property on this object
            MyComboBox.DataContext = this;
        }
    

    I think it goes something like that. I cant test it right now but hopefully it will nudge you in the right direction.

    Edit: If you want to try the “other way” of binding heres how. Expand the SelectedItem binding to look like this:

            <ComboBox
                Name="MyComboBox"
                DisplayMemberPath="LastName"
                SelectedItem="{Binding MySelected,
                    RelativeSource={RelativeSource FindAncestor,
                        AncestorType={x:Type Window}}}" />
    

    You can now skip setting the DataContext in code behind:

        public MainWindow()
        {
            InitializeComponent();
            // ...
    
            // this will cause the "MySelected" binding to target the correct property on this object
            //MyComboBox.DataContext = this;
        }
    

    This is because that FindAncestor mode makes the ComboBox itself find the object to which property it should bind, rather than you specifically stating.

    The current hot topic here at the office is which of these two ways are the best. To me its just more XAML and less code behind (or the other way around), just use the method that places the code where youre comfortable to work. I think there are some scenarios where the latter is preferred (like when you include data binding controls inside other controls), but Im just dabbling so I havent really figured those parts out yet.

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

Sidebar

Related Questions

Can anyone help me in the following question please? Define a macro which has
Can anyone explain me the difference of all and also an another question does
I was hoping if anyone can answer a fundamental question that I have regarding
Have a rather simple question. Does anyone knows if i can use jparallax both
Can anyone please provide me in layman's terms the difference between developing a JRuby
Short question: Can anyone tell me what the requirements (especially when it comes to
Question 1 Can anyone tell me if there is any difference between following 2
Can anyone help me with a question about webservices and scalability? I have written
Can anyone help me with the question. I'm trying to get msmq messages count
Anyone who can answer my question deserves a BIG FAT GOLD MEDAL OF AWESOMENESS!

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.