wpf自定义样式DatePickerStyle及Calendar控件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

 <Style x:Key="DatePickerStyle1" TargetType="{x:Type DatePicker}">
            <Setter Property="Foreground" Value="#FF333333"/>
            <Setter Property="IsTodayHighlighted" Value="True"/>
            <Setter Property="SelectedDateFormat" Value="Short"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Padding" Value="2"/>
            <Setter Property="BorderBrush">
                <Setter.Value>
                    <LinearGradientBrush EndPoint=".5,0" StartPoint=".5,1">
                        <GradientStop Color="#FFA3AEB9" Offset="0"/>
                        <GradientStop Color="#FF8399A9" Offset="0.375"/>
                        <GradientStop Color="#FF718597" Offset="0.375"/>
                        <GradientStop Color="#FF617584" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DatePicker}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" CornerRadius="2">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_DisabledVisual"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid x:Name="PART_Root" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="Stretch" Margin="0,0,0,0" >
                                <Grid.Resources>
                                    <SolidColorBrush x:Key="DisabledBrush" Color="#A5FFFFFF"/>
                                <ControlTemplate x:Key="DropDownButtonTemplate" TargetType="{x:Type Button}">
                                    <Grid Background="#FF0C274B" FlowDirection="LeftToRight" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="32" Height="32">
                                        <Path Fill="#26c9ff"  HorizontalAlignment="Center"  Stretch="Fill" VerticalAlignment="Center"  Data="M 890.127 145.672 h -43.389 V 132.78 c 0 -38.644 -31.466 -70.234 -69.922 -70.234 h -1.032 c -38.458 0 -69.922 31.59 -69.922 70.234 v 12.892 h -386.7 V 132.78 c 0 -38.644 -31.465 -70.234 -69.922 -70.234 h -1.063 c -38.457 0 -69.923 31.59 -69.923 70.234 v 12.892 h -43.296 c -38.458 0 -69.923 31.622 -69.923 70.207 v 671.446 c 0 38.644 31.466 70.236 69.923 70.236 h 755.17 c 38.458 0 69.922 -31.592 69.922 -70.236 V 215.879 c 0 -38.585 -31.464 -70.207 -69.923 -70.207 Z M 733.83 132.78 c 0 -23.222 18.793 -42.139 41.954 -42.139 h 1.032 c 23.13 0 41.954 18.918 41.954 42.139 v 124.927 c 0 23.224 -18.824 42.14 -41.954 42.14 h -1.032 c -23.161 0 -41.954 -18.916 -41.954 -42.14 V 132.779 Z m -527.607 0 c 0 -23.222 18.793 -42.139 41.955 -42.139 h 1.063 c 23.098 0 41.954 18.918 41.954 42.139 v 124.927 c 0 23.224 -18.856 42.14 -41.954 42.14 h -1.063 c -23.162 0 -41.955 -18.916 -41.955 -42.14 V 132.779 Z m 683.904 768.626 H 134.958 c -7.585 0 -13.985 -6.525 -13.985 -14.08 V 393.649 h 783.138 v 493.676 c 0 7.555 -6.399 14.08 -13.984 14.08 Z" />
                                        <Path Fill="#26c9ff" Margin="7" HorizontalAlignment="Center" Stretch="Fill" VerticalAlignment="Center"  Data="M 223.755 494.2 H 352.78 v 63.488 H 223.755 V 494.2 Z M 449.035 494.2 h 128 v 63.488 h -128 V 494.2 Z M 673.291 494.2 h 125.952 v 63.488 H 673.291 V 494.2 Z M 223.755 623.224 H 352.78 v 62.464 H 223.755 v -62.464 Z M 449.035 623.224 h 128 v 62.464 h -128 v -62.464 Z M 673.291 623.224 h 125.952 v 62.464 H 673.291 v -62.464 Z M 223.755 749.176 H 352.78 v 65.536 H 223.755 v -65.536 Z M 449.035 749.176 h 128 v 65.536 h -128 v -65.536 Z M 673.291 749.176 h 125.952 v 65.536 H 673.291 v -65.536 Z" StrokeThickness="2"/>
                                    </Grid> 
                                    </ControlTemplate>
                                </Grid.Resources>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Button x:Name="PART_Button" Grid.Column="1" Foreground="{TemplateBinding Foreground}" Focusable="False" HorizontalAlignment="Left" Margin="3,0,3,0" Grid.Row="0" Template="{StaticResource DropDownButtonTemplate}" VerticalAlignment="Center" Width="30"/>
                            <DatePickerTextBox x:Name="PART_TextBox" Grid.Column="0"  Foreground="{TemplateBinding Foreground}" Focusable="{TemplateBinding Focusable}" HorizontalContentAlignment="Stretch" Grid.Row="0" VerticalContentAlignment="Center" Style="{DynamicResource DatePickerTextBoxStyle1}" Background="{x:Null}" Margin="0,0,0,0" />
                                <Grid x:Name="PART_DisabledVisual" Grid.ColumnSpan="2" Grid.Column="0" IsHitTestVisible="False" Opacity="0" Grid.Row="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Rectangle Grid.Column="0" Fill="#A5FFFFFF" RadiusY="1" Grid.Row="0" RadiusX="1"/>
                                    <Rectangle Grid.Column="1" Fill="#A5FFFFFF" Height="32" Margin="3,0,3,0" RadiusY="1" Grid.Row="0" RadiusX="1" Width="32"/>
                                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_TextBox}" StaysOpen="False"/>
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- 应该在此定义资源字典条目。-->
    <Style x:Key="DatePickerTextBoxStyle1" TargetType="{x:Type DatePickerTextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DatePickerTextBox}">
                    <TextBox BorderThickness="0" Text="{Binding Path=SelectedDate, StringFormat='yyyy-MM-dd', RelativeSource={RelativeSource AncestorType=DatePicker}}" Foreground="White" VerticalContentAlignment="Center" Padding="5,0,5,0" Background="{x:Null}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
<!--日历样式-->
        <Style x:Key="DefaultCalendar" TargetType="{x:Type Calendar}">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Foreground" Value="#26c9ff"/>
            <Setter Property="Background" Value="#041e37"/>
            <Setter Property="BorderBrush" Value="#26c9ff"/>
           <Setter Property="CalendarDayButtonStyle" Value="{DynamicResource CalendarDayButtonStyle1}" />
          <Setter Property="CalendarButtonStyle" Value="{DynamicResource CalendarButtonStyle1}" /> 
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="IsTodayHighlighted" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Calendar}">
                        <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" 
                                      FontFamily="{TemplateBinding FontFamily}"
                                      BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"
                                      FontSize="20" Style="{DynamicResource CalendarItemStyle1}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
 <!--Day按钮样式-->
        <Style x:Key="CalendarDayButtonStyle1" TargetType="{x:Type CalendarDayButton}">
            <Setter Property="MinWidth" Value="28" />
            <Setter Property="MinHeight" Value="5" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Margin" Value="0" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Width" Value="48"/>
            <Setter Property="Height" Value="35"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CalendarDayButton}">
                        <Grid x:Name="Grid" Margin="{TemplateBinding Margin}">
                            <Border x:Name="Bg" Background="{TemplateBinding Background}" CornerRadius="3" Margin="1" BorderThickness="1" BorderBrush="{x:Null}"/>
                            <ContentPresenter x:Name="NormalText" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          Margin="5,2,5,2" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          TextElement.Foreground="#26c9ff" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#7F000000" TargetName="Bg"></Setter>
                            <Setter Property="BorderBrush" Value="#FF26c9ff" TargetName="Bg"></Setter>
                            <Setter Property="TextElement.Foreground" Value="#FFF" TargetName="NormalText"></Setter>
                            </Trigger>
                            <Trigger Property="IsToday" Value="True">
                            <Setter Property="Background" Value="#7F000000" TargetName="Bg"></Setter>
                            <Setter Property="BorderBrush" Value="#FF26c9ff" TargetName="Bg"></Setter>
                            <Setter Property="TextElement.Foreground" Value="#FFF" TargetName="NormalText"></Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#7F000000" TargetName="Bg"></Setter>
                            <Setter Property="BorderBrush" Value="#FF26c9ff" TargetName="Bg"></Setter>
                            <Setter Property="TextElement.Foreground" Value="#FFF" TargetName="NormalText"></Setter>
                        </Trigger>
                            <!--不可用日期-->
                            <Trigger Property="IsBlackedOut" Value="True">
                                <Setter Property="Opacity" Value="0.4" TargetName="Grid"></Setter>
                            </Trigger>
                            <!--不在当月的日期-->
                            <Trigger Property="IsInactive" Value="True">
                                <Setter Property="Opacity" Value="0.4" TargetName="Grid"></Setter>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" Value="0.4" TargetName="Grid"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
<Style x:Key="CalendarButtonStyle1" TargetType="{x:Type CalendarButton}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="MinWidth" Value="40" />
        <Setter Property="MinHeight" Value="42" />
        <Setter Property="FontSize" Value="20" />
        <Setter Property="Width" Value="80" />
        <Setter Property="Height" Value="60" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarButton}">
                    <Grid x:Name="Grid" Margin="{TemplateBinding Margin}">
                        <Border x:Name="Bg" Background="{TemplateBinding Background}" CornerRadius="3" Margin="1" BorderThickness="1" BorderBrush="{x:Null}"/>
                        <ContentPresenter x:Name="NormalText" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          Margin="5,2,5,2" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          TextElement.Foreground="#26c9ff" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="#7F000000" TargetName="Bg"></Setter>
                            <Setter Property="BorderBrush" Value="#FF26c9ff" TargetName="Bg"></Setter>
                            <Setter Property="TextElement.Foreground" Value="#FFF" TargetName="NormalText"></Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#7F000000" TargetName="Bg"></Setter>
                            <Setter Property="BorderBrush" Value="#FF26c9ff" TargetName="Bg"></Setter>
                            <Setter Property="TextElement.Foreground" Value="#FFF" TargetName="NormalText"></Setter>
                        </Trigger>
                        <!--不在当月的日期-->
                        <Trigger Property="IsInactive" Value="True">
                            <Setter Property="Opacity" Value="0.4" TargetName="Grid"></Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" Value="0.4" TargetName="Grid"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值