WPF自訂控制項與樣式(5)-Calendar/DatePicker日期控制項自訂樣式及擴充(轉)

來源:互聯網
上載者:User

標籤:

一.前言

  申明:WPF自訂控制項與樣式是一個系列文章,前後是有些關聯的,但大多是按照由簡到繁的順序逐步發布的等,若有不明白的地方可以參考本系列前面的文章,文末附有部分文章連結。

本文主要內容:

  • 日曆控制項Calendar自訂樣式;
  • 日期控制項DatePicker自訂樣式,及Label標籤、浮水印、清除日期功能擴充;

二.Calendar自訂樣式

先看看效果:

從上面圖可以看出,日曆的顯示其實有三種狀態,如下面的分解圖:

  • "日"部分的顯示;
  • "月"部分的顯示;
  • "年"部分的顯示;

  

因此一個完整的日曆,就包含多個部分,首先是"日"按鈕的樣式:  

 View Code

日曆日期面板樣式:  

 View Code

年月按鈕樣式:  

 View Code

最後綜合以上的樣式,定義我們需要的Calendar樣式就差一步之遙了。  

    <!--預設日曆樣式-->    <Style x:Key="DefaultCalendar" TargetType="{x:Type Calendar}">        <Setter Property="SnapsToDevicePixels" Value="True" />        <Setter Property="Foreground" Value="{StaticResource TextForeground}" />        <Setter Property="CalendarDayButtonStyle" Value="{StaticResource CalendarDayButtonStyle}" />        <Setter Property="CalendarItemStyle" Value="{StaticResource CalendarItemStyle}" />        <Setter Property="CalendarButtonStyle" Value="{StaticResource CalendarButtonStyle}" />        <Setter Property="Background" Value="{StaticResource PopupBackground}" />        <Setter Property="BorderBrush" Value="{StaticResource ControlBorderBrush}" />        <Setter Property="BorderThickness" Value="1" />        <Setter Property="FontSize" Value="13" />        <Setter Property="FontFamily" Value="{StaticResource FontFamily}" />        <Setter Property="IsTodayHighlighted" Value="True" />        <Setter Property="Template">            <Setter.Value>                <ControlTemplate TargetType="{x:Type Calendar}">                    <StackPanel x:Name="PART_Root" HorizontalAlignment="Center" Background="Transparent">                        <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" FontSize="{TemplateBinding FontSize}"                                      FontFamily="{TemplateBinding FontFamily}"                                      BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"                                      Style="{TemplateBinding CalendarItemStyle}" />                    </StackPanel>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>

 

三.DatePicker自訂樣式,及Label標籤、浮水印、清除日期功能擴充

  有了上面的日曆樣式,下面做DatePicker的樣式就好辦了,其實就是TextBox+Button+Calendar。此處的實現同上一篇(WPF自訂控制項與樣式(3)-TextBox & RichTextBox & PasswordBox樣式)思路基本一致,因此本文就不做更多的解釋,可以參考本系列前面的文章(末尾附錄有連結)先看看:

3.1 DatePicker自訂樣式

基本樣式定義:  

 View Code

浮水印效果:

使用樣本:

            <DatePicker Margin="3" core:ControlAttachProperty.Watermark="媽的快輸入日期"/>            <DatePicker Margin="3" SelectedDate="{x:Static system:DateTime.Today}"/>            <DatePicker Margin="3" IsEnabled="False" SelectedDate="{x:Static system:DateTime.Today}"/>

3.2 Label標籤

通過擴充基礎樣式中的標籤附加屬性實現,定義樣式代碼:  

    <!--DatePicker包含附加屬性Label的樣式 LabelDatePicker -->    <Style TargetType="{x:Type DatePicker}" x:Key="LabelDatePicker" BasedOn="{StaticResource DefaultDatePicker}">        <Setter Property="Width" Value="260"></Setter>        <Setter Property="local:ControlAttachProperty.LabelTemplate" >            <Setter.Value>                <ControlTemplate TargetType="ContentControl">                    <Border Width="60" Background="{StaticResource TextLabelBackground}">                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Right" Margin="2" Text="{TemplateBinding Content}"></TextBlock>                    </Border>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>

:

使用樣本:  

<DatePicker Margin="3" Style="{StaticResource LabelClearButtonDatePicker}" core:ControlAttachProperty.Watermark="選擇出生日期" core:ControlAttachProperty.Label="出生日期"/><DatePicker Margin="3" Style="{StaticResource LabelDatePicker}" core:ControlAttachProperty.Label="死亡日期" SelectedDate="{x:Static system:DateTime.Today}"/><DatePicker Margin="3" Style="{StaticResource LabelDatePicker}" core:ControlAttachProperty.Label="非法日期" IsEnabled="False" SelectedDate="{x:Static system:DateTime.Today}"/>

3.3 清除日期值的功能擴充

此功能在前面的文章有介紹過(參考本文末尾連結),效果在上面的圖片中可以看到。樣式代碼:  

    <!--DatePicker包含清除Text按鈕的樣式 ClearButtonDatePicker -->    <Style TargetType="{x:Type DatePicker}" x:Key="ClearButtonDatePicker" BasedOn="{StaticResource DefaultDatePicker}">        <Setter Property="local:ControlAttachProperty.AttachContent">            <Setter.Value>                <ControlTemplate>                    <local:FButton FIcon="&#xe60a;" Style="{StaticResource FButton_Transparency}" IsTabStop="False" FIconMargin="0"                                   local:ControlAttachProperty.IsClearTextButtonBehaviorEnabled="True" Command="local:ControlAttachProperty.ClearTextCommand"                                    CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DatePicker}}}"                               Margin="1,3,0,4" FIconSize="14" Foreground="{StaticResource TextForeground}" Cursor="Arrow"/>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>    <!--DatePicker包含附加屬性Label,以及ClearText按鈕的樣式 LabelClearButtonDatePicker -->    <Style TargetType="{x:Type DatePicker}" x:Key="LabelClearButtonDatePicker" BasedOn="{StaticResource DefaultDatePicker}">        <Setter Property="Width" Value="280"></Setter>        <Setter Property="local:ControlAttachProperty.LabelTemplate" >            <Setter.Value>                <ControlTemplate TargetType="ContentControl">                    <Border Width="60" Background="{StaticResource TextLabelBackground}">                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Right" Margin="2" Text="{TemplateBinding Content}"></TextBlock>                    </Border>                </ControlTemplate>            </Setter.Value>        </Setter>        <Setter Property="local:ControlAttachProperty.AttachContent">            <Setter.Value>                <ControlTemplate>                    <local:FButton FIcon="&#xe60a;" Style="{StaticResource FButton_Transparency}" IsTabStop="False" FIconMargin="0"                               local:ControlAttachProperty.IsClearTextButtonBehaviorEnabled="True" Command="local:ControlAttachProperty.ClearTextCommand"                                CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DatePicker}}}"                               Margin="0,3,0,4" FIconSize="14" Foreground="{StaticResource TextForeground}" Cursor="Arrow"/>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>

使用樣本:  

<DatePicker Margin="3" Style="{StaticResource ClearButtonDatePicker}"/><DatePicker Margin="3" Style="{StaticResource LabelClearButtonDatePicker}" core:ControlAttachProperty.Watermark="選擇出生日期" core:ControlAttachProperty.Label="出生日期"/>

 

附錄:參考引用

WPF自訂控制項與樣式(1)-向量字型表徵圖(iconfont)

WPF自訂控制項與樣式(2)-自訂按鈕FButton

WPF自訂控制項與樣式(3)-TextBox & RichTextBox & PasswordBox樣式、浮水印、Label標籤、功能擴充

WPF自訂控制項與樣式(4)-CheckBox/RadioButton自訂樣式

 

著作權,文章來源:http://www.cnblogs.com/anding

個人能力有限,本文內容僅供學習、探討,歡迎指正、交流。

WPF自訂控制項與樣式(5)-Calendar/DatePicker日期控制項自訂樣式及擴充(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.