I have a simple ListBox in a WPF app bound to a view model, so the XAML looks like:
<ListBox ItemsSource="{Binding Drivers}"
SelectedItem="{Binding SelectedDriver, Mode=TwoWay}" />
In my view model, the SelectedDriver is set, so I expect when the view displays, the Driver in the list box which correlates to the SelectedDriver should be highlighted but it is not.
What do I need to do to get the SelectedItem to be highlighted when the SelectedItem value is set in the view model?
SOLVED: Based on the answers below, I realized that while my SelectedDriver object was in fact being set, it did not belong to the Drivers collection. Once I corrected this, it is working as expected. Thanks to all for your input. I don't have enough points to increment the points on answers but thanks very much.
edit: The view model does implement INotifyPropertyChanged. Many other items in the view are properly displayed based on their bindings.
edit: The view model constructor does some housekeeping, and one of the things it does is set up various view elements to prior states. One of those elements is the Drivers list box, so in the view model code, it is being set to an actual item just as you describe: SelectedDriver = Drivers.Where(d => d.Id == savedId) I have verified in Debug that this code is finding the correct Driver and the value of SelectedDriver is correct, it is just not being highlighted in the UI.
INotifyPropertyChanged
? – Rohit Vats