Hi,
I am implementing one application in WPF MVVM.
I am having WPF application and DLL class library.
For example in application I am having 2 combobox's
I am binding database servers to the 1st combobox.
based on the 1st combobox selection then I am binding the 2nd combobox with list of all the databases.
my problem is here when we select the server from the 1st combobox it keep open state and Application gets hangs some time. I am unable to do other operations at that particular time.
My XAML code below :
<ComboBox Grid.Column="2"
Grid.Row="1" Grid.ColumnSpan="3" MaxHeight="25"
ItemsSource="{Binding Servers}"
SelectedValue="{Binding SelectedServer}"
Name="cboServer"
DataContext="{Binding Source={StaticResource vm}}" />
<ComboBox Grid.Column="2"
Grid.Row="2" Grid.ColumnSpan="3" MaxHeight="25"
ItemsSource="{Binding Databases}"
SelectedValue="{Binding SelectedDatabase}"
Name="cboDatabase"
DataContext="{Binding Source={StaticResource vm}}"/>
My view model code as follows:
private ObservableCollection<string> _Servers;
private string _SelectedServer;
public ObservableCollection<string> Servers
{
get { return _Servers; }
set
{
_Servers = value;
OnPropertyChanged("Servers");
}
}
public string SelectedServer
{
get { return _SelectedServer; }
set
{
_SelectedServer = value;
OnPropertyChanged("SelectedServer");
_extractInfo.ServerName = !string.IsNullOrEmpty(_SelectedServer) ? _SelectedServer : null;
if (!string.IsNullOrEmpty(_extractInfo.ServerName))
GetDatabases();
else
Databases = new ObservableCollection<string>();
}
}
private ObservableCollection<string> _DataBaseList;
private string _SelectedDatabase;
public ObservableCollection<string> Databases
{
get { return _DataBaseList; }
set
{
_DataBaseList = value;
OnPropertyChanged("Databases");
}
}
private ObservableCollection<string> GetDatabases()
{
try
{
Databases = new ObservableCollection<string>(ExtractHelper.RequestList(_extractInfo, ExtractHelper.RequestTypes.Databases).AsEnumerable().Select(x => x.DatabaseName));
return Databases;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
Kindly let me know is there any wrong with the code, if its wrong let me know how we achieve without freeze the application
What I have tried:
I have tried using Dispatcher like below.
When I am using below code, 1st combobox gets closed when I select the server, but application gets freezes some time then its displaying the databases list in the 2nd combobox.
public string SelectedServer
{
get { return _SelectedServer; }
set
{
_SelectedServer = value;
OnPropertyChanged("SelectedServer");
_extractInfo.ServerName = !string.IsNullOrEmpty(_SelectedServer) ? _SelectedServer : null;
if (!string.IsNullOrEmpty(_extractInfo.ServerName))
Application.Current.Dispatcher.BeginInvoke(
new Action(() =>
{
GetDatabases();
}),
DispatcherPriority.Background,
null
);
else
Databases = new ObservableCollection<string>();
}
}