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

如何在不修改源码的情况下扩展WPF DateTimePicker下拉列表高度

问题描述

我使用Xceed WPF Toolkit DateTimePicker控件实现日期和时间一体化选择,代码如下:

<xctk:DateTimePicker x:Name="DTCalendar" Margin="10,10,10,0" VerticalAlignment="Top" ValueChanged="DTCalendar_ValueChanged" Width="234" Height="34" Format="Custom" FormatString="dd ddd yyyy, HH:mm" FontSize="14" FontWeight="Bold" HorizontalContentAlignment="Center"></xctk:DateTimePicker>

目前遇到的问题是TimePicker的下拉列表高度过短,请问是否可以在不修改底层源码的前提下将其高度扩展至最大?

解决方案

可以通过自定义控件样式调整TimePicker下拉列表的高度,无需修改控件源码。核心思路是找到DateTimePicker内部TimePicker的弹出框(Popup)模板,调整它的高度限制:

  1. 获取默认样式模板:先用Blend工具或查阅Xceed官方文档,获取DateTimePicker的默认样式,定位到其中嵌套的TimePicker控件的Popup部分。
  2. 自定义样式覆盖:在XAML资源中添加自定义样式,给TimePicker的Popup设置较大的MaxHeight(比如600),或者直接设为Double.PositiveInfinity取消高度上限。

示例代码

方式一:完整重写TimePicker模板调整高度

<Window.Resources>
    <!-- 自定义TimePicker样式,调整下拉Popup高度 -->
    <Style TargetType="xctk:TimePicker">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="xctk:TimePicker">
                    <Grid>
                        <xctk:ButtonSpinner x:Name="PART_Spinner"
                                            AllowSpin="{TemplateBinding AllowSpin}"
                                            IsTabStop="False"
                                            ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
                            <xctk:WatermarkTextBox x:Name="PART_TextBox"
                                                   HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                   VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                                   IsReadOnly="{TemplateBinding IsReadOnly}"
                                                   Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"
                                                   Watermark="{TemplateBinding Watermark}"/>
                        </xctk:ButtonSpinner>
                        <Popup x:Name="PART_Popup"
                               AllowsTransparency="True"
                               Placement="Bottom"
                               StaysOpen="False"
                               MaxHeight="600"> <!-- 按需调整高度值 -->
                            <Border Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                                    BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}"
                                    BorderThickness="1">
                                <xctk:TimePickerPanel x:Name="PART_TimePickerPanel"
                                                      Background="{TemplateBinding Background}"
                                                      BorderBrush="{TemplateBinding BorderBrush}"
                                                      BorderThickness="{TemplateBinding BorderThickness}"
                                                      FirstHour="{TemplateBinding FirstHour}"
                                                      LastHour="{TemplateBinding LastHour}"
                                                      MinuteInterval="{TemplateBinding MinuteInterval}"
                                                      SelectedTime="{Binding SelectedTime, RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Border>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<!-- 你的DateTimePicker控件 -->
<xctk:DateTimePicker x:Name="DTCalendar" Margin="10,10,10,0" VerticalAlignment="Top" ValueChanged="DTCalendar_ValueChanged" Width="234" Height="34" Format="Custom" FormatString="dd ddd yyyy, HH:mm" FontSize="14" FontWeight="Bold" HorizontalContentAlignment="Center"></xctk:DateTimePicker>

方式二:继承默认样式只修改高度

如果不想完整重写模板,可以继承默认样式,仅修改Popup的MaxHeight:

<Style TargetType="xctk:TimePicker" BasedOn="{StaticResource {x:Type xctk:TimePicker}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="xctk:TimePicker">
                <Grid>
                    <xctk:ButtonSpinner x:Name="PART_Spinner"
                                        AllowSpin="{TemplateBinding AllowSpin}"
                                        IsTabStop="False"
                                        ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
                        <xctk:WatermarkTextBox x:Name="PART_TextBox"
                                               HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                               VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                               IsReadOnly="{TemplateBinding IsReadOnly}"
                                               Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"
                                               Watermark="{TemplateBinding Watermark}"/>
                    </xctk:ButtonSpinner>
                    <Popup x:Name="PART_Popup"
                           AllowsTransparency="True"
                           Placement="Bottom"
                           StaysOpen="False"
                           MaxHeight="{x:Static SystemParameters.MaximizedPrimaryScreenHeight}"> <!-- 使用屏幕最大高度 -->
                        <Border Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                                BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}"
                                BorderThickness="1">
                            <xctk:TimePickerPanel x:Name="PART_TimePickerPanel"
                                                  Background="{TemplateBinding Background}"
                                                  BorderBrush="{TemplateBinding BorderBrush}"
                                                  BorderThickness="{TemplateBinding BorderThickness}"
                                                  FirstHour="{TemplateBinding FirstHour}"
                                                  LastHour="{TemplateBinding LastHour}"
                                                  MinuteInterval="{TemplateBinding MinuteInterval}"
                                                  SelectedTime="{Binding SelectedTime, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Border>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

注意事项

  • 确保项目中已引用Xceed WPF Toolkit的命名空间:xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
  • 如果希望下拉列表高度无上限,直接把MaxHeight设为Double.PositiveInfinity即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 15:27:12