Pergunta

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();
}
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top