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?
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
Userclass would look something like this:You should probably do the same for your
testclass, as well.WPF will watch for when the
PropertyChangedevent is fired, and updates any affected bindings as needed. This should cause the selected item in theComboBoxto 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
DataContextfor theWindowis), but here’s what I got working:I created a class called
ViewModel, which is set to theDataContextof the mainWindow. Here’s its code:I moved the creation of the two lists to code. One thing I noticed in your sample is that one instance of
ListOfTestswas used as theItemsSourceof theComboBox, while another instance was used to buildListOfUsers. 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
Windowis the following:The key to getting things working is the
CurrentUserproperty. It is bound toListBox.SelectedItem, andComboBox.SelectedItemis bound toCurrentUser.UserTest. This will change the selection in theComboBoxto represent the test of the user selected in theListBox.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.