---
lang: ja-jp
breaks: true
---
# WPF MaterialDesignThemes の Calendar をカスタマイズ 2021-05-20
> WPFのCalendarコントロールをカスタマイズ
> https://iyemon018.hatenablog.com/entry/2016/05/10/231206
> Calendar Styles and Templates
> https://docs.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/cc278077(v=vs.95)?redirectedfrom=MSDN
:::info
当初、出来るだけ`<Style BasedOn="" ><Setter /></Style>` または、コードビハインドを使用して対応しようとしたが、`DataTemplate`及び`Resources`の書き換えが必要となり困難だったため、Styleを再定義する方向で一旦は対応。
:::
## カスタマイズ前の Calendar

## カスタイズ後の Calendar

## App.xaml
```xml=
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Light"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />
<ResourceDictionary
Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary
Source="pack://application:,,,/WpfApp10;component/Style/Calendar.xaml" />
<ResourceDictionary
Source="pack://application:,,,/WpfApp10;component/Style/DatePicker.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
```
## MainWindow.xaml
```xml=
<Grid>
<StackPanel
Orientation="Vertical">
<Calendar
Language="ja-JP" />
<DatePicker
Language="ja-JP" />
</StackPanel>
</Grid>
```
## Style/Calendar.xaml
```xml=
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
xmlns:globalization="clr-namespace:System.Globalization;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApp10"
>
・・・
<local:DateTimeToDayOfWeekBrushConverter
x:Key="DateTimeToDayOfWeekBrushConverter" />
<Style
TargetType="{x:Type wpf:MaterialDateDisplay}">
<Setter
Property="Background"
Value="Transparent" />
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type wpf:MaterialDateDisplay}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel
Orientation="Vertical"
Margin="0">
<TextBlock
x:Name="ComponentThreeTextBlock"
Text="{TemplateBinding ComponentThreeContent}"
HorizontalAlignment="Left"
FontSize="15"
FontWeight="Normal" />
<StackPanel
x:Name="ComponentOneTwoWrapper"
Orientation="Horizontal"
HorizontalAlignment="Left">
<TextBlock
Text="{TemplateBinding ComponentTwoContent}"
FontSize="25"
FontWeight="Normal" />
<TextBlock
Text="{TemplateBinding ComponentOneContent}"
Margin="10 0 0 0"
FontSize="25"
FontWeight="Normal" />
</StackPanel>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger
Property="IsDayInFirstComponent"
Value="True">
<Setter
TargetName="ComponentThreeTextBlock"
Property="Opacity"
Value=".56" />
</Trigger>
<Trigger
Property="IsEnabled"
Value="False">
<Setter
TargetName="ComponentThreeTextBlock"
Property="Opacity"
Value="1" />
<Setter
TargetName="ComponentOneTwoWrapper"
Property="Opacity"
Value=".56" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style
x:Key="MaterialDesignCalendarButton"
TargetType="{x:Type CalendarButton}">
・・・
<Setter
Property="FontSize"
Value="25" />
・・・
<Setter
Property="Margin"
Value="0" />
<Setter
Property="Width"
Value="70" />
<Setter
Property="Height"
Value="70" />
・・・
</Style>
<Style
x:Key="MaterialDesignCalendarDayButton"
TargetType="{x:Type CalendarDayButton}">
・・・
<Setter
Property="Foreground"
Value="{Binding Converter={StaticResource DateTimeToDayOfWeekBrushConverter}, Mode=OneWay}" />
・・・
<Setter
Property="FontSize"
Value="25" />
・・・
<Setter
Property="Margin"
Value="0" />
<Setter
Property="Width"
Value="40" />
<Setter
Property="Height"
Value="40" />
・・・
</Style>
<Style
x:Key="MaterialDesignCalendarItemPortrait"
TargetType="{x:Type CalendarItem}">
・・・
<Setter
Property="Margin"
Value="0" />
・・・
<DataTemplate
x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
<TextBlock
Opacity="0.65"
FontWeight="Bold"
FontSize="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,6,0,6"
Text="{Binding [0]}">
<TextBlock.Style>
<Style
TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger
Binding="{Binding }"
Value="日">
<Setter
Property="Foreground"
Value="{Binding Source={StaticResource DateTimeToDayOfWeekBrushConverter}, Path=SundayBrush, Mode=OneWay}" />
</DataTrigger>
<DataTrigger
Binding="{Binding }"
Value="土">
<Setter
Property="Foreground"
Value="{Binding Source={StaticResource DateTimeToDayOfWeekBrushConverter}, Path=SaturdayBrush, Mode=OneWay}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
・・・
<Button
x:Name="PART_HeaderButton"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="3"
FontWeight="Bold"
Focusable="False"
FontSize="10.5"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Template="{StaticResource HeaderButtonTemplate}"
Visibility="{TemplateBinding wpf:CalendarAssist.IsHeaderVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Height="Auto"
Margin="5 5" />
<TextBlock
x:Name="CurrentDateTextBlock"
Grid.Row="1"
Grid.Column="0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="13 8 8 8"
FontSize="20"
FontWeight="SemiBold"
Opacity="0.56"
RenderTransformOrigin="0, 0.5">
・・・
</Style>
<Style
TargetType="{x:Type Calendar}"
BasedOn="{StaticResource MaterialDesignCalendarPortrait}" />
</ResourceDictionary>
```
## Style/DatePicker.xaml
```xml=
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf">
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<Style
x:Key="MaterialDesignDatePickerTextBox"
TargetType="{x:Type DatePickerTextBox}">
・・・
</Style>
<Style
x:Key="MaterialDesignDatePickerCalendarPortrait"
TargetType="{x:Type Calendar}">
・・・
</Style>
<Style
x:Key="MaterialDesignDatePicker"
TargetType="{x:Type DatePicker}">
・・・
</Style>
<Style
x:Key="MaterialDesignFloatingHintDatePicker"
TargetType="{x:Type DatePicker}"
BasedOn="{StaticResource MaterialDesignDatePicker}">
・・・
</Style>
<Style
x:Key="MaterialDesignFilledDatePicker"
TargetType="{x:Type DatePicker}"
BasedOn="{StaticResource MaterialDesignFloatingHintDatePicker}">
・・・
</Style>
<Style
x:Key="MaterialDesignOutlinedDatePicker"
TargetType="{x:Type DatePicker}"
BasedOn="{StaticResource MaterialDesignFloatingHintDatePicker}">
・・・
</Style>
<Style
TargetType="{x:Type DatePicker}"
BasedOn="{StaticResource MaterialDesignDatePicker}" />
</ResourceDictionary>
```
## DateTimeToDayOfWeekBrushConverter.cs
> https://github.com/Iyemon-018/Dev/blob/master/Samples/CalendarSample1/CalendarSample1/DateTimeToDayOfWeekBrushConverter.cs
###### tags: `WPF` `MaterialDesignThemes` `Calendar` `XAML`