[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>
'C# > WPF' 카테고리의 다른 글
[WPF] Enum Type 을 ComboBox에 ItemSource로 바인딩 예제 (1) | 2024.09.16 |
---|---|
[WPF] TrayIcon 생성 시에 Resource 의 이미지 설정 하기 (0) | 2024.04.02 |
[WPF] Title Bar에 최소화,최대화,닫기 버튼 숨기는 방안 (0) | 2024.03.21 |