[WPF] WPF ProgressBar 사용하기(프로그레스바)

안녕하세요.

 

오늘은 WPF에서 ProgressBar 컨트롤을 사용하는 방법에 대해서 알려 드리려고 합니다.

 

ProgressBar 컨트롤은 보통 어떤 상태의 진행 상황을 사용자에게 시각적으로 보여줄 때 자주 사용하는 컨트롤인데요.

 

WPF에서 어떻게 코드로 구현하고 사용하는지 보여 드리도록 하겠습니다.

 

 

Xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<Window x:Class="ProgressbarTest.MainWindow"
        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:ProgressbarTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="400">
    <StackPanel>
        <Button x:Name="uiBtn_Start" Content="시작" Width="100" Height="20" 
                Margin="10"
                Click="uiBtn_Start_Click">
        </Button>
        
        <!--ProgressBar 컨트롤-->
        <Grid Width="200" Margin="10">
 
            <ProgressBar x:Name="uiPb_Main"  VerticalAlignment="Center" Height="30"
                     Minimum="0" Maximum="100" Value="30"/>
            <TextBlock Text="{Binding ElementName=uiPb_Main,Path=Value,StringFormat={}{0:0}%}"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"/>
        </Grid>
    </StackPanel>
</Window>
 
cs

 

비하인드 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace ProgressbarTest
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        BackgroundWorker _worker = null;
 
        public MainWindow()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 프로그레스바 컨트롤 증가 버튼 이벤트 핸들러
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiBtn_Start_Click(object sender, RoutedEventArgs e)
        {
            _worker = new BackgroundWorker();
            _worker.WorkerReportsProgress = true;
            _worker.DoWork += _worker_DoWork;
            _worker.ProgressChanged += _worker_ProgressChanged;
            _worker.RunWorkerCompleted += _worker_RunWorkerCompleted;
            _worker.RunWorkerAsync();
        }
 
        /// <summary>
        /// DoWorker 스레드 이벤트 핸들러
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                _worker.ReportProgress(i); //값을 ReportProgress 매개변수로 전달
                Thread.Sleep(100); //0.1초
            }
        }
 
        private void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            uiPb_Main.Value = e.ProgressPercentage;
        }
 
        /// <summary>
        /// 프로그레스바 컨트롤 작업 끝났을 때
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            uiPb_Main.Value = uiPb_Main.Maximum;
        }
    }
}
 
cs

 

실행 결과

 

위와 같이 ProgressBar 컨트롤이 제대로 동작하는 것을 확인하실 수 있습니다.

 

감사합니다.^^

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY