WPF Datepicker样式
时间: 2023-07-20 09:37:23 浏览: 172
WPF 的 DatePicker 控件的样式可以通过自定义控件模板来实现,以下是一些示例代码:
1. 在XAML文件中定义DatePicker的控件模板:
```
<Style TargetType="DatePicker">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="2,0,0,0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DatePicker">
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="DisabledBrush" Color="#A5FFFFFF" />
<ControlTemplate x:Key="DropDownButtonTemplate" TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" To="#FF448DCA" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="arrow" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Duration="0" To="#FF2C628B" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="arrow" />
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="arrow" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border x:Name="Background" Grid.ColumnSpan="2" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="2" />
<Border Grid.Column="0" Margin="1" Background="White" CornerRadius="2,0,0,2" />
<Path x:Name="arrow" Grid.Column="1" Fill="#FF606060" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z">
<Path.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Path.RenderTransform>
</Path>
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid x:Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" Background="White" BorderThickness="1" BorderBrush="#FF707070" />
<ScrollViewer>
<StackPanel>
<DatePicker x:Name="PART_TextBox" Style="{x:Null}" />
</StackPanel>
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<DatePickerTextBox x:Name="PART_TextBox" Grid.Column="0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Focusable="False" HorizontalContentAlignment="Stretch" IsReadOnly="True" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch" />
<Button Grid.Column="1" Focusable="False" IsEnabled="{TemplateBinding IsDropDownOpen}" Template="{StaticResource DropDownButtonTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
2. 在使用DatePicker时,直接使用该控件模板即可:
```
<DatePicker Style="{StaticResource {x:Type DatePicker}}" />
```
以上是一些示例代码,你可以根据自己的需求自定义DatePicker的样式。
阅读全文