【问题标题】:WPF: DatePicker text centered verticallyWPF:DatePicker 文本垂直居中
【发布时间】:2015-06-06 09:30:29
【问题描述】:

我在我的应用中使用 DatePicker,定义如下:

<DatePicker Width="200"
            Margin="20, 20, 20, 0"
            SelectedDate="{Binding PeriodEndDate, Mode=TwoWay}" />

以下是日期选择器的显示方式:

如何让日期 (4/22/2015) 在文本框中垂直居中(日期周围的虚线是实际的文本框边界)?

我尝试将VerticalContentAlignmentVerticalAlignment 都设置为Center,但这不会影响日期居中。

如果我使用 Snoops 深入研究日期选择器,我可以在 DatePicker 中看到一个名为 PART_TextBox 的元素。如果我将这个PART_TextBox VerticalContentAlignment 更改为Center,文本将居中(默认设置为Stretch)。但是,我不知道如何访问这个子组件来更改它的VerticalContentAlignment

【问题讨论】:

标签: wpf xaml textbox datepicker


【解决方案1】:

我猜这是你字体大小的产物。试试吧。

<DatePicker Width="200"
            FontSize="8"
            Margin="20, 20, 20, 0"
            SelectedDate="{Binding PeriodEndDate, Mode=TwoWay}" >

    <DatePicker.Resources>
        <Style TargetType="DatePickerTextBox">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </DatePicker.Resources>

</DatePicker>

【讨论】:

    猜你喜欢
    • 2013-10-15
    • 2015-09-16
    • 1970-01-01
    • 2023-03-05
    • 2014-03-02
    • 2011-02-03
    • 2014-10-22
    相关资源
    最近更新 更多