In My WPF application, I have One Mainwindow with two user controls.
My
Mainwindow.xaml like this
<Grid Name="MainGrid">
<my:Usercontrol1 HorizontalAlignment="Left" Margin="70,132,0,0" Name="dummy1" VerticalAlignment="Top" />
</Grid>
Then my
UserControl.Xaml
Here I am having one grid contains controls which is connecting to the database, it will take time to load, for that I am using one loadingUserControl like loading rotating image.
<Grid Height="300" Width="770" >
<Grid Height="223" Width="334" Margin="10,80,346,-20">
<Label Width="90" Height="24" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontFamily="Verdana" FontWeight="Normal" Margin="10,37,6,5" Grid.Column="1">Sql Server</Label>
<Grid Grid.Column="2" Width="200" removed="White" Height="20" Margin="1,39,20,7">
<ComboBox VerticalAlignment="Top" HorizontalAlignment="Left" removed="Transparent" Style="{StaticResource StyleForWidth200}" Margin="0,0,0,0" Name="cmbServerName" Width="200" Height="20" FontFamily="Verdana" IsEditable="True" StaysOpenOnEdit="True" ClipToBounds="False">
</ComboBox>
</Grid>
<RadioButton Content="Trusted Connection" Grid.Column="2" Height="20" HorizontalAlignment="Left" Name="rbTrustedConnection" VerticalAlignment="Stretch" Width="200" FontFamily="Verdana" FontWeight="Normal" Margin="0,6,0,16" Grid.Row="1" Grid.RowSpan="2" FontSize="10" IsChecked="True" />
<RadioButton Content="SQL Server Authentication" Grid.Column="2" Grid.Row="2" Height="16" HorizontalAlignment="Left" Name="rbSQLServerAuthentication" VerticalAlignment="Stretch" Width="200" FontFamily="Verdana" FontWeight="Normal" Margin="0,5,0,0" FontSize="10" />
<Label Width="90" Height="24" Grid.Row="3" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontFamily="Verdana" FontWeight="Normal" Grid.Column="1" Margin="10,9,6,9">User Name</Label>
<TextBox Grid.Column="2" Grid.Row="3" Name="txtUserName" Width="200" Height="20" HorizontalAlignment="Left" Text="" FontFamily="Verdana" FontWeight="Normal" Margin="0,11"></TextBox>
<Label Width="90" Height="24" Grid.Row="4" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontFamily="Verdana" FontWeight="Normal" Grid.Column="1" Margin="10,2,6,2">Password</Label>
<PasswordBox Grid.Column="2" Grid.Row="4" Width="200" Height="20" HorizontalAlignment="Left" FontFamily="Verdana" Name="txtPwdWord"/>
<Button Grid.Column="2" Grid.Row="5" Name="btnConnect" Width="70" Height="30" Margin="131,0,20,5" Content="Connect" FontFamily="Verdana" FontWeight="Normal" Focusable="True" Style="{StaticResource NormalStyle11}" Click="btnConnect_Click"></Button>
<my:LoadingControl x:Name="loadingControl1" Margin="0,70,0,0" Visibility="Visible" />
</Grid>
</Grid>
In My code behind
UserControl1.cs
here I am using threading
when its loading database, LoadingControl should be rotate,once database loaded loadingControl should be hidden, for that I implemented the following code.
here my problem is LoadingControl is not roatating and application get hangs, but when the database loaded lodingcontrol being hidden.
can you please figure out what I missed here.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
BindInstances = new Thread(new ThreadStart(ServerConnection));
BindInstances.Start();
}
private void ServerConnection()
{
if (Dispatcher.CheckAccess())
{
BindDefaultInstances();
loadingControl1.Visibility = Visibility.Hidden;
}
else
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal,new Action(ServerConnection));
}
private void BindDefaultInstances()
{
DataAccess objIED_DAL = new DataAccess();
DataTable dtInstanceList = new DataTable();
dtInstanceList = objIED_DAL.GetInstances();
if (dtInstanceList.Rows.Count > 0)
{
foreach (DataRow dr in dtInstanceList.Rows)
{
cmbServerName.Items.Add(dr["ServerName"]);
}
}
}