You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wpf Material Design日期选择器主题自定义:无需改源码修改配色

实现方案(无需修改原始源码)

当然可以不用碰Material Design的原始源码啦!WPF的样式系统就是专门用来做这种自定义配色的——咱们只需要在你的XAML的资源区域(比如Window.Resources或者UserControl.Resources)里加几段针对性的样式代码,就能实现你要的所有颜色需求。下面是具体的实现步骤:

1. 先搞定Calendar的整体基础配色

先设置Calendar的主背景为黑色,默认文本为白色,也可以顺便去掉默认边框让风格更统一:

<Style TargetType="Calendar">
    <Setter Property="Background" Value="Black"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="BorderThickness" Value="0"/>
</Style>

2. 修改Calendar的标题栏(月份选择栏)

这部分对应你要的「标题栏背景灰色、文本白色、图标白色」需求,咱们重写CalendarItem的模板来覆盖默认样式:

<Style TargetType="CalendarItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CalendarItem">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <!-- 标题栏背景设为灰色,文本白色 -->
                        <CalendarButton x:Name="PART_HeaderButton" 
                                        Background="Gray" 
                                        Foreground="White"
                                        Grid.Row="0" 
                                        Style="{DynamicResource MaterialDesignCalendarButton}"/>
                        <!-- 日历主体区域 -->
                        <Grid x:Name="PART_MonthView" Grid.Row="1" Margin="6">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <CalendarView x:Name="PART_CalendarView" Grid.Row="1"/>
                            <!-- 左右切换箭头图标设为白色 -->
                            <CalendarButton x:Name="PART_PreviousButton" 
                                            Foreground="White"
                                            HorizontalAlignment="Left" 
                                            Grid.Row="0" 
                                            Style="{DynamicResource MaterialDesignCalendarButton}"/>
                            <CalendarButton x:Name="PART_NextButton" 
                                            Foreground="White"
                                            HorizontalAlignment="Right" 
                                            Grid.Row="0" 
                                            Style="{DynamicResource MaterialDesignCalendarButton}"/>
                            <!-- 月份年份文本设为白色 -->
                            <ContentControl x:Name="PART_Title" 
                                            Foreground="White"
                                            HorizontalAlignment="Center" 
                                            Grid.Row="0" 
                                            Style="{DynamicResource MaterialDesignCalendarTitle}"/>
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

3. 细化日期单元格的文本颜色

如果需要确保日历里的每个日期文本都是白色,可以添加这个样式:

<Style TargetType="CalendarDayButton">
    <Setter Property="Foreground" Value="White"/>
    <!-- 可选:你也可以自定义选中日期的背景色,比如改成深灰或者蓝色 -->
    <Setter Property="Background" Value="#FF4080"/>
</Style>

4. 如果你说的「标题栏」是DialogHost的对话框标题

如果你的需求里的标题栏指的是DialogHost弹出的对话框顶部标题(而不是Calendar的标题),那再添加这个样式来修改DialogHeader的配色:

<Style TargetType="materialDesign:DialogHost">
    <Setter Property="DialogHeaderStyle">
        <Setter.Value>
            <Style TargetType="materialDesign:DialogHeader">
                <Setter Property="Background" Value="Gray"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="IconForeground" Value="White"/>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

小提示

  • 把这些样式放在你的XAML的<Window.Resources>或者<UserControl.Resources>标签内部,确保Calendar和DialogHost控件能读取到这些样式。
  • 不同版本的Material Design控件模板可能有细微差异,但核心的PART_前缀部件名称基本是一致的,如果遇到适配问题,只需要微调模板里的部件属性即可。
  • 所有修改都是基于WPF的样式覆盖机制,完全不需要改动Material Design的原始库代码。

内容的提问来源于stack exchange,提问作者Qwe Qwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:16:25