I have a handful of situations where I am trying to design my WPF .NET 4.0 application to handle a change of a SelectedItem in a ListBox/ListView.
Essentially I want to query other data to fill an ObservableCollection/DataGrid based on what a user has selected. My problem is that by having everything on the same thread, the responsiveness suffers inasmuch as the item that is clicked only shows as being Selected once any code started in the "SelectedItem" Setter has finished and this "wait" doesn't respond how I want it to.
The data being queried normally fills an ObservableCollection via LINQ from the same database which is why everything is using the same UI thread.
Ideally I want:
- A user to click on an item in a list which is selected immediately without the feeling of the application hanging.
- An "IsBusy" property to be set to true (used in bindings to change the enabled status of certain controls).
- Start querying other data and cnce the that is finished, a (e.g.) DataGrid should be populated and the IsBusy is returned to false.
What is the best way to achieve this with secondary data loading "in the background"? I've used BackgroundWorker in the past in WinForms but am eager to learn more about the Dispatcher which I think is the right direction.
My SelectedItem property is as straightforward as this:
public Employee SelectedEmployee
{
get
{
return mvSelectedEmployee;
}
set
{
mvSelectedEmployee = value;
RaisePropertyChanged();
IsBusy = true;
QueryMyEmployeesAddressesAndOtherData(); //which takes sometimes 2 seconds
IsBusy = false;
}
}
I want the user to know something is happening but smoothly despite there being a slight delay.
Thank you.