WPF ProgressBar & Dim Controls on Operation
I'm looking to implement a progress bar (on my applications status bar) and at the same time dim all the MainWindow controls except minimize/maximize/close window buttons when I execution.
Right now, I have a Progress Bar User Control with a Background worker process but it pops the progress bar control in a separate window. Here is the XAML for my status bar:
StatusBar x:Name="StatusB" Grid.Column="0" Grid.Row="5" Width="Auto" Height="30" Background="DarkGray" DockPanel.Dock="Bottom">
<StackPanel>
开发者_Python百科 <Label Content="Status:" Foreground="White" Height="30" Width="50" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" ></Label>
</StackPanel>
<StackPanel>
<local:ProgressB></local:ProgressB>
</StackPanel>
</StatusBar>
Here is my Progress Bar User Control:
<Grid Width="250" Height="25">
<Label x:Name="lblProgress" Content="0%" Width="25" Height="20" Margin="155,2,70,0" Foreground="White" IsEnabled="False" Visibility="Hidden"></Label>
<ProgressBar x:Name="progress" Height="20" IsIndeterminate="False" Width="151" HorizontalAlignment="Left" Margin="0,2,0,0"></ProgressBar>
<Button x:Name="btnCancel" Width="50" Height="20" HorizontalAlignment="Right" Click="btnCancel_Click" Content="Cancel" Margin="0,2,0,0" IsEnabled="False" Visibility="Hidden"></Button>
</Grid>
Background Worker:
private void ExecuteBuild(object sender, ExecutedRoutedEventArgs e)
{
//Launching the bind/train process requires a separate proc
ProcessStartInfo startInfo = new ProcessStartInfo("app.exe", filePath);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
// Borrowed for build iterations (you need 3, minimum)
int maxRecords = 3;
pd = new ProgressB();
// Cancel Build
pd.Cancel += CancelProcess;
// Add multithread dispatcher
System.Windows.Threading.Dispatcher pdDispatcher = pd.Dispatcher;
//create our background worker and also have a cancel button
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
// Updates the progress text
UpdateProgressDelegate update = new UpdateProgressDelegate(UpdateProgressText);
int x = 0;
for (x = 1; x <= maxRecords; x++)
{
if (worker.CancellationPending)
{
args.Cancel = true;
return;
}
System.Threading.Thread.Sleep(1);
When I have it pop a dialog window it works fine but when I put it in my status bar (see first XAML ref) it shows no progress whatsoever -- however the background process does work still. Any suggestions?
The reason you're not seeing updates is probably that UpdateProgress is not called on the UI thread. Your code sample is cut off, so it's hard to say. Use BackgroundWorker.ReportProgress for reporting progress. This will be called on the UI thread. Some samples here.
精彩评论