Visual Studio/WPF > 컨트롤 > DatePicker > TextBox + Calendar

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

@ WPF 4.5 입문 by 오타 카즈키
No.3884/9985

DatePicker 컨트롤은 TextBox 컨트롤과 Calendar 컨트롤을 결합한 것과 같은 컨트롤입니다.

DatePicker와 Calendar를 조합해 보았다.
DatePicker에서 선택한 날짜가 Calendar에 표시되도록 구현했습니다.

v0.1



XAML
<Window x:Class="_170425_t1710_dataPicker.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:_170425_t1710_dataPicker"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DatePicker x:Name="datePicker1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,26,0,0" Width="200" SelectedDateChanged="datePicker1_SelectedDateChanged"/>
        <Calendar x:Name="calendar1" HorizontalAlignment="Left" Margin="280,22,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 _170425_t1710_dataPicker
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            calendar1.SelectedDate = datePicker1.SelectedDate;
        }
    }
}



같은 달 안에 있으면 선택한 날짜가 달력에 파란색으로 표시됩니다.
다른 달을 선택할 때는 달력 측의 표시월은 변경되지 않고 4월 그대로이다.

v0.2 > 다른 달 대응



다른 달을 선택시 달력 표시를 변경하려고 시도했다.
참고 : h tp : / / s t c ゔ ぇ rf ぉ w. 코 m / 쿠에 s Chion s / 24245523 / Getchin g-te-fu rst-an d-o-st- y-o-a-mon th-u-shin g-a-gi-da- 메오 b ぇ ct

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 _170425_t1710_dataPicker
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var selected = (DateTime)datePicker1.SelectedDate;
            calendar1.SelectedDate = datePicker1.SelectedDate;
            var firstDayOfMonth = new DateTime(selected.Year, selected.Month, 1);
            calendar1.DisplayDateStart = firstDayOfMonth;
        }
    }
}

좋은 웹페이지 즐겨찾기