&tag(WPF/プログレスバー);
<Window x:Class="ControlDemo.DialogDemo.ProgressDialogBasic.ProgressDialogBasicWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ControlDemo.DialogDemo.ProgressDialogBasic"
mc:Ignorable="d"
Loaded="UserControl_Loaded"
Title="プログレスバー付きのダイアログ" Height="140" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ProgressBar x:Name="progressBar" Height="25" Margin="10" Minimum="0" Maximum="100" Value="50"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10">
<Button x:Name="buttonStart" Width="100" Click="Button_Click">開始</Button>
<Button x:Name="buttonFinish" Width="100" Margin="10,0,0,0" Click="Button_Click_1" IsEnabled="False">終了</Button>
</StackPanel>
</Grid>
</Window>
namespace ControlDemo.DialogDemo.ProgressDialogBasic
{
/// <summary>
/// ProgressDialogBasicWindow.xaml の相互作用ロジック
/// </summary>
public partial class ProgressDialogBasicWindow : Window
{
private BackgroundWorker backgroundWorker = new BackgroundWorker();
private ProgressDialogBasicVM viewModel;
public ProgressDialogBasicWindow()
{
InitializeComponent();
this.viewModel = new ProgressDialogBasicVM();
DataContext = viewModel;
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
backgroundWorker.ProgressChanged += ProgressChanged;
backgroundWorker.DoWork += DoWork;
// not required for this question, but is a helpful event to handle
backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 50; i <= 100; i++)
{
if (backgroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
Thread.Sleep(200);
backgroundWorker.ReportProgress(i);
}
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// This is called on the UI thread when ReportProgress method is called
progressBar.Value = e.ProgressPercentage;
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("Cancel");
} else
{
MessageBox.Show("Completed");
}
// This is called on the UI thread when the DoWork method completes
// so it's a good place to hide busy indicators, or put clean up code
buttonStart.IsEnabled = true;
buttonFinish.IsEnabled = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
buttonStart.IsEnabled = false;
buttonFinish.IsEnabled = true;
backgroundWorker.RunWorkerAsync();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
backgroundWorker.CancelAsync();
buttonStart.IsEnabled = true;
buttonFinish.IsEnabled = false;
}
}
}