Can I Do The Following With ListBox.SelectedItem?
-
Good people, I have a listbox filled with Employee objects. Each object has employee data (e.g. name, salary, rank, birthdate, etc...). When the listbox displays, about half the time it will need to show which employee was selected on a previous screen by having that employee selected. The listbox will contain the list of all the employees. Of course, the data context for the listbox is AllEmployees: an observable collection of employee objects. So, can I do one or both of the following? ListBoxEmployees.SelectedItem = Employee; or ListBoxEmployees.SelectedItem = Employee as ListBoxItem; I am hoping that it will automatically find and select the appropriate employee object in its collection if I assign it the employee selected on a previous screen. Any help or information you can provide would be greatly appreciated. Thanks, Blitz
-
Good people, I have a listbox filled with Employee objects. Each object has employee data (e.g. name, salary, rank, birthdate, etc...). When the listbox displays, about half the time it will need to show which employee was selected on a previous screen by having that employee selected. The listbox will contain the list of all the employees. Of course, the data context for the listbox is AllEmployees: an observable collection of employee objects. So, can I do one or both of the following? ListBoxEmployees.SelectedItem = Employee; or ListBoxEmployees.SelectedItem = Employee as ListBoxItem; I am hoping that it will automatically find and select the appropriate employee object in its collection if I assign it the employee selected on a previous screen. Any help or information you can provide would be greatly appreciated. Thanks, Blitz
BlitzPackage wrote:
ListBoxEmployees.SelectedItem = Employee;
Yes, assuming Employee is a reference to an employee object in the collection which is the ItemsSource for the listbox.
BlitzPackage wrote:
ListBoxEmployees.SelectedItem = Employee as ListBoxItem;
No. Invalid cast there.
Mark Salsbery Microsoft MVP - Visual C++ :java: