2
votes

I have written a small WPF application to edit a config file. The GUI mainly consists of two ListBoxes. The content of the second ListBox depends on the selected item of the first ListBox and should update whenever another item in the first ListBox is selected. I realized this using the SelectionChanged event of the first ListBox to set the ItemsSource of the second one. Built it and tested it on my machines. Worked (and still is working) fine. Then I sent the application to my colleague so he could try the application. On his machine the second ListBox was not updating (when he first selected an item from the first ListBox the second one changed as expected but not if he then selected a other item).

I narrowed the problem down and found out that the event was raised as expected but the SelectedItem property was not changing.

I was able to solve the problem by using the AddedItems property of the SelectionChangedEventArgs, which is updating correctly instead.

Does anyone know why this happens? Furthermore the selected item is highlighted correctly. So does WPF use another property to determine which item to highlight? And if so how can they get out of sync?

In case anyone asks here are the relevant code fragments

The ListBox:

<ListBox Name="lb_Users" SelectionChanged="lb_Users_SelectionChanged">
    <ListBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Remove" Click="UserRemove_Click"/>
        </ContextMenu>
    </ListBox.ContextMenu>
</ListBox>

And the updated Handler:

private void lb_Users_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if((e.AddedItems[0] as User)!= null)
    lb_VMs.ItemsSource = (e.AddedItems[0] as User).GetVMs();
}
1
Have you guys checked that you have the same version of .NET, with all the same hotfixes/updates for Windows installed?Geeky Guy
@Renan Sometimes you forget the most simple things... He has an older version than the one I use. So is this a known bug in .NET 4.03 because I couldn't find anything on this from googeling?mgttlinger
Probably. Don't forget that with Windows Update working automatically (as it should), nearly no one should have that kind of problem.Geeky Guy

1 Answers

1
votes

As seen in the comments, the problem is a version mismatch. Hope this helps whomever is having this kind of problem, since it seems there is about no documentation on that.