본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - datepicker 날짜계산 ( 달력 )

728x90
반응형
 WPF & C# - datepicker 날짜계산 ( 달력 )

datepicker.zip
0.41MB

 

MainWindow.xaml

 

 
1
2
3
<DatePicker x:Name="dtp01" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" SelectedDateChanged="dtp_SelectedDateChanged"/>
<DatePicker x:Name="dtp02" HorizontalAlignment="Left" Margin="101,10,0,0" VerticalAlignment="Top" SelectedDateChanged="dtp_SelectedDateChanged"/>
<Label x:Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="10,39,0,0" VerticalAlignment="Top" Height="74" Width="177"/>
cs
 

 

 

 

MainWindow.xaml.cs

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private void dtp_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    // 1번 날짜가 선택 시
    if (dtp01.SelectedDate.HasValue)
    {
        dtp02.DisplayDateStart = dtp01.SelectedDate.Value;   // datepicker02 에 시작날짜 선택 제한
    }
 
    // 2번 날짜 선택 시
    if (dtp02.SelectedDate.HasValue)
    {
        dtp01.DisplayDateEnd = dtp02.SelectedDate.Value;   // datepicker01 에 끝 날짜 선택 제한
    }
 
    // 기간표기(총 일수)
    if (dtp01.SelectedDate.HasValue && dtp02.SelectedDate.HasValue)   // 날짜가 둘다 있는지 여부확인
    {
        lbl.Content = dtp01.SelectedDate.Value.ToString("yyyy-MM-dd");   // 선택 날짜 표기
        lbl.Content += " ~ " + dtp02.SelectedDate.Value.ToString("yyyy-MM-dd"+ "\n";   // 선택 날짜 표기
        lbl.Content += "총 기간은" + (dtp02.SelectedDate.Value - dtp01.SelectedDate.Value).Days.ToString() + "일 입니다.";   // days 체크하여 표기
    }
}
cs
 

 

 

 

728x90
반응형