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 299955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:52:13+00:00 2026-05-12T06:52:13+00:00

I’ve been looking around for an answer to my question for a few days

  • 0

I’ve been looking around for an answer to my question for a few days now, but am not able to find a solution.

The problem is that the combobox updates the Test object in User class with the the previous selected Users’.

i.e. you select user2 and user2 has test2, you then select user5 that has test5. Now if you select user2 again, it will show that it has test5.

Here is some code. I have two classes Users and Tests. And two ObservableCollections for each of those. This is how I’ve got them setup:

public class User
{
    public string Name { get; set; }
    public int test { get; set; }
    public test userTest { get; set; }
}

public class test
{
    public int ID { get; set; }
    public String Name { get; set; }
}

public class ListOfTests:ObservableCollection<test>
{
    public ListOfTests()
    {
        for (int i = 0; i < 4; i++)
        {
            test newTest = new test();
            newTest.ID = i;
            newTest.Name = "Test " + i;
            Add(newTest);
        }
    }
}

public class ListOfUsers: ObservableCollection<User>
{
    public ListOfUsers()
    {
        ListOfTests testlist = new ListOfTests();
        for (int i = 0; i < 10; i++)
        {
            User newUser = new User();
            newUser.Name = "User " + i;
            newUser.ID = i;
            newUser.userTest = testlist[i];
            Add(newUser);
        }
    }
}

And the XAML is:

<Window x:Class="ComboboxTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ComboboxTest"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="SP1">
    <StackPanel.Resources>
        <local:ListOfTests x:Key="ListOfTests" />
    </StackPanel.Resources>
    <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
    <TextBox Text="{Binding Path=Name}" Foreground="Black"  />
    <TextBox Text="{Binding Path=userTest}" />  
    <ComboBox SelectedItem="{Binding Path=userTest}" 
              SelectedValue="{Binding Path=userTest.ID}"
              ItemsSource="{Binding Source={StaticResource ListOfTests}}" 
              DisplayMemberPath="Name" 
              SelectedValuePath="ID"

              Foreground="Black" />
</StackPanel>

Now if I change the Binding on the SelectedItem to “{Binding Path=userTest, Mode=OneWay}” then it works, but i can not change the it manually.

Here is a kicker thought… If I target .Net 4.0 (VS2010) then it works fine…

Can anyone please help me find a solution to this?

  • 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-12T06:52:13+00:00Added an answer on May 12, 2026 at 6:52 am

    If I’m understanding your question, it sounds like that WPF isn’t being notified when the value of a property changes. You can get around this by implementing the INotifyPropertyChanged interface. For example, the User class would look something like this:

    public class User : INotifyPropertyChanged
    {
        private string name = string.Empty;
        public string Name
        {
            get { return this.name; }
            set
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
    
        private int test = 0;
        public int Test
        {
            get { return this.test; }
            set
            {
                this.test = value;
                this.OnPropertyChanged("Test");
            }
        }
    
        private test userTest = null;
        public test UserTest
        {
            get { return this.userTest; }
            set
            {
                this.userTest = value;
                this.OnPropertyChanged("UserTest");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            PropertyChangedEventHandler eh = this.PropertyChangd;
            if(null != eh)
            {
                eh(this, new PropertyChangedEventArgs(propName));
            }
        }
    }
    

    You should probably do the same for your test class, as well.

    WPF will watch for when the PropertyChanged event is fired, and updates any affected bindings as needed. This should cause the selected item in the ComboBox to change back to the test for user2.

    Update: OK, I think I got this working. I think that you’re missing part of the code in what you posted (like what the DataContext for the Window is), but here’s what I got working:

    I created a class called ViewModel, which is set to the DataContext of the main Window. Here’s its code:

    class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            for(int i = 0; i < 4; i++)
            {
                this.tests.Add(new Test()
                {
                    ID = i,
                    Name = "Test " + i.ToString(),
                });
            }
    
            for(int i = 0; i < 4; i++)
            {
                this.users.Add(new User()
                {
                    Name = "User " + i.ToString(),
                    ID = i,
                    UserTest = this.tests[i],
                });
            }
        }
    
        private ObservableCollection<User> users = new ObservableCollection<User>();
        public IEnumerable<User> Users
        {
            get { return this.users; }
        }
    
        private ObservableCollection<Test> tests = new ObservableCollection<Test>();
        public IEnumerable<Test> Tests
        {
            get { return this.tests; }
        }
    
        private User currentUser = null;
        public User CurrentUser
        {
            get { return this.currentUser; }
            set
            {
                this.currentUser = value;
                this.OnPropertyChanged("CurrentUser");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var eh = this.PropertyChanged;
            if(null != eh)
            {
                eh(this, new PropertyChangedEventArgs(propName));
            }
        }
    }
    

    I moved the creation of the two lists to code. One thing I noticed in your sample is that one instance of ListOfTests was used as the ItemsSource of the ComboBox, while another instance was used to build ListOfUsers. I’m not sure if that was part of the problem or not, but it is better to just have one list of tests.

    The XAML for the main Window is the following:

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Window1" Height="300" Width="300">
        <StackPanel>
            <ListBox ItemsSource="{Binding Path=Users}"
                     SelectedItem="{Binding Path=CurrentUser}"
                     DisplayMemberPath="Name"
                     IsSynchronizedWithCurrentItem="True">
            </ListBox>
            <TextBox Text="{Binding Path=CurrentUser.Name}" />
            <TextBox Text="{Binding Path=CurrentUser.UserTest.Name}" />
            <ComboBox ItemsSource="{Binding Path=Tests}"
                      SelectedItem="{Binding Path=CurrentUser.UserTest}"
                      DisplayMemberPath="Name" />
        </StackPanel>
    </Window>
    

    The key to getting things working is the CurrentUser property. It is bound to ListBox.SelectedItem, and ComboBox.SelectedItem is bound to CurrentUser.UserTest. This will change the selection in the ComboBox to represent the test of the user selected in the ListBox.

    I got this all working using Visual Studio 2008 SP1, so hopefully it will work for you as well. If you have any problems getting this working, let me know and I’ll see what I can do.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.