えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

2009年7月8日 #

WPF Carendar DatePicker 土日祝日 の 色替え

WPF Carendar 土日祝日の色替え
http://blogs.msdn.com/kathykam/archive/2009/05/18/how-to-customize-holiday-appearance-in-the-silverlight-calendar-jason-cooke.aspx

DatePicker はCalendarStyleしかもっていないので厄介です。
DatePicker でできなきゃというわけでやってみました。
Holidays は実際のアプリでは Window1.cs から注入するんでしょうが今回はコンストラクタで今年分だけ。

<Window x:Class="DatePickerDemo.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300"

    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"

    xmlns:local="clr-namespace:DatePickerDemo"

    >

  <Window.Resources>

    <local:DatePickerBackgroundConverter x:Key="DatePickerBackgroundConverter" />

    <Style TargetType="toolkit:Calendar" x:Key="CalendarStyle1">

      <Setter Property="Template">

        <Setter.Value>

          <ControlTemplate TargetType="toolkit:Calendar">

            <StackPanel HorizontalAlignment="Center" x:Name="Root">

              <toolkit:Calendar x:Name="Calendar" SelectedDate="{TemplateBinding SelectedDate}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">

                <toolkit:Calendar.CalendarDayButtonStyle>

                  <Style TargetType="toolkit:CalendarDayButton">

                    <Setter Property="Template">

                      <Setter.Value>

                        <ControlTemplate TargetType="toolkit:CalendarDayButton">

                          <Grid Background=

            "{Binding Converter={StaticResource DatePickerBackgroundConverter}, Path=Date}">

                            <ContentControl x:Name="Content" Margin="5,1,5,1"

            Content="{TemplateBinding Content}" />

                          </Grid>

                        </ControlTemplate>

                      </Setter.Value>

                    </Setter>

                  </Style>

                </toolkit:Calendar.CalendarDayButtonStyle>

              </toolkit:Calendar>

            </StackPanel>

          </ControlTemplate>

        </Setter.Value>

      </Setter>

    </Style>

  </Window.Resources>

    <Grid>

    <toolkit:DatePicker Height="25" Name="datePicker1" VerticalAlignment="Top" CalendarStyle="{StaticResource CalendarStyle1}"/>

  </Grid>

</Window>

 

using System;

using System.Collections.Generic;

using System.Windows.Media;

 

namespace DatePickerDemo

{

    class DatePickerBackgroundConverter : System.Windows.Data.IValueConverter

    {

        public static IList<DateTime> Holidays { get; set; }

        public DatePickerBackgroundConverter()

        {

            Holidays = new DateTime[] {

                new DateTime(2009, 1,1),

                new DateTime(2009, 1,12),

                new DateTime(2009, 2,11),

                new DateTime(2009, 3,20),

                new DateTime(2009, 4,29),

                new DateTime(2009, 5,3),

                new DateTime(2009, 5,4),

                new DateTime(2009, 5,5),

                new DateTime(2009, 5,6),

                new DateTime(2009, 7,20),

                new DateTime(2009, 9,21),

                new DateTime(2009, 9,22),

                new DateTime(2009, 9,23),

                new DateTime(2009, 10,12),

                new DateTime(2009, 11,3),

                new DateTime(2009, 11,23),

                new DateTime(2009, 12,23)

            };

 

        }

        public object Convert(object value,

                            Type targetType,

                            object parameter,

                            System.Globalization.CultureInfo culture)

        {

            DateTime date = (DateTime)value;

            if (date.DayOfWeek == DayOfWeek.Sunday ||

                        (Holidays != null && Holidays.Contains(date)))

            {

                return new SolidColorBrush(Colors.LightPink);

            }

            else if (date.DayOfWeek == DayOfWeek.Saturday)

            {

                return new SolidColorBrush(Colors.LightBlue);

            }

            return new SolidColorBrush(Colors.White);

        }

 

        public object ConvertBack(object value, Type targetType,

        object parameter, System.Globalization.CultureInfo culture)

        {

            return null;

        }

    }

}

 

posted @ 20:33 | Feedback (57)