I have a scenario where I have a globally accessible Properties window (similar to the Properties window in Visual Studio) that is associated with the SelectedObject property of my model. I have several different ways to view and select objects, so the first attempt is to associate them with SelectedObject directly. For instance:
<ListBox ItemsSource="{Binding ActiveProject.Controllers}" SelectedItem="{Binding SelectedObject, Mode=TwoWay}"/> <ListBox ItemsSource="{Binding ActiveProject.Machines}" SelectedItem="{Binding SelectedObject, Mode=TwoWay}"/>
This works well when I have more than one item in each list, but it fails if there is only one item in the list. When I select an item, SelectedObject is not updated, since the list still believes that its original item was selected. I believe this is because the two-way binding simply ignores the update from the source when SelectedObject is not an object in the list, leaving the SelectedItem list unchanged. In this way, the bindings become out of sync.
Does anyone know how to make sure that their SelectedItem is reset in the list when SelectedObject is not in the list? Is there a better way to do this so as not to suffer from this problem?
source share