最近重写了WPF自带的DatePicker控件样式,可响应鼠标滚轮、键盘等事件修改日期,效果如图:
话不多说,直接上源码:
1、主窗体xaml:
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded">
<Window.Resources>
<!--RepeatButton样式-->
<Style x:Key="ArrowButtonStyle" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Grid >
<Border Background="#f3f3f3" CornerRadius="5,5,0,0" BorderBrush="#D3D3D3D3" BorderThickness="1,1,1,0"></Border>
<Path Fill="#FF5A5A5A" Stretch="Fill" Stroke="{x:Null}" Data="M135.66667,4.6666667 L132.16599,9.4999781 139.16621,9.4999778 z" Width="6" Height="4" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Red"/>
<Setter Property="Opacity" Value="0.2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--DatePickerTextBox样式-->
<Style TargetType="DatePickerTextBox">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="YearText" AllowDrop="False" IsReadOnly="True"
BorderThickness="0,0,0,0" Margin="2,2,2,2"
Text="{Binding Path=SelectedDate, StringFormat='yyyy', RelativeSource={RelativeSource AncestorType=DatePicker}}"
GotFocus="YearText_GotFocus" LostFocus="YearText_LostFocus"
MouseWheel="Text_MouseWheel" PreviewKeyDown="Text_PreviewKeyDown"/>
<TextBlock Text="/" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="MonthText" AllowDrop="False" IsReadOnly="True"
BorderThickness="0,0,0,0" Margin="2,2,2,2"
Text="{Binding Path=SelectedDate, StringFormat='MM', RelativeSource={RelativeSource AncestorType=DatePicker}}"
GotFocus="MonthText_GotFocus" LostFocus="MonthText_LostFocus"
MouseWheel="Text_MouseWheel" PreviewKeyDown="Text_PreviewKeyDown"/>
<TextBlock Text="/" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="DayText" AllowDrop="False" IsReadOnly="True"
BorderThickness="0,0,0,0" Margin="2,2,2,2"
Text="{Binding Path=SelectedDate, StringFormat='dd', RelativeSource={RelativeSource AncestorType=DatePicker}}"
GotFocus="DayText_GotFocus" LostFocus="DayText_LostFocus"
MouseWheel="Text_MouseWheel" PreviewKeyDown="Text_PreviewKeyDown"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--DatePicker样式-->
<Style 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" Value="#D3D3D3D3"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Template">