본문 바로가기
C# /WPF

[WPF] DatePicker Calendar 특정 일자 Day Button Style 변경

by Hwoarang757 2022. 8. 25.

출처 : WPF: Enable Specific Dates Only In a DatePicker - TechNet Articles - United States (English) - TechNet Wiki (microsoft.com)

 

WPF: Enable Specific Dates Only In a DatePicker - TechNet Articles - United States (English) - TechNet Wiki

 

social.technet.microsoft.com

 

[WPF] DatePicker Calendar 특정 일자 Day Button Style 변경 

2022년 8월 21,22,23,25일자의 Day Button의 Style을 다르게 표시 하고자 하는 기능이 필요 하였다.

 

 

CalendarTest.cs 작성 진행 

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace CalendarTest
{
    public class DateTimeConverter : IValueConverter
    {
        // Style을 적용할 특정 일자 HashSet
        private static readonly HashSet<DateTime> s_datesToBeEnabled = new HashSet<DateTime>
        {
            new DateTime(2022,8,25),
            new DateTime(2022,8,21),
            new DateTime(2022,8,22),
            new DateTime(2022,8,23),
            new DateTime(2022,7,27)
        };
        
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => s_datesToBeEnabled.Contains((DateTime)value);


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
            throw new NotSupportedException();
    }
}

 

Xaml 에 DatePicker 컨트롤 추가 진행 

<Window x:Class="CalendarTest.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:CalendarTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Loaded="Window_Loaded"
        
        >
    <Window.DataContext>
        <local:DateTimeConverter />
    </Window.DataContext>
    <Grid>

        <DatePicker Name="dtPicker">
            <DatePicker.CalendarStyle>
                <Style TargetType="Calendar">
                    <Setter Property="CalendarDayButtonStyle">
                        <Setter.Value>
                            <Style TargetType="CalendarDayButton">
                                <Style.Resources>
                                    <local:DateTimeConverter x:Key="DateTimeConverter"/>
                                </Style.Resources>

                                <Setter Property="IsHitTestVisible" Value="True" />
                                <Setter Property="Opacity" Value="0.5"/>


                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Converter={StaticResource DateTimeConverter}}" Value="True">
                                        <Setter Property="IsHitTestVisible" Value="True" />
                                        <Setter Property="Opacity" Value="1" />
                                        <Setter Property="Background" Value="SkyBlue"/>
                                        <Setter Property="FontStyle" Value="Oblique"/>
                                        <Setter Property="FontWeight" Value="Bold"/>

                                    </DataTrigger>
                                    
                                    
                                </Style.Triggers>
                                

                            </Style>
                        </Setter.Value>
                    </Setter>
                </Style>
                
            </DatePicker.CalendarStyle>

        </DatePicker>

    </Grid>
</Window>