Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: DatePicker text centered vertically

I am using a DatePicker in my app defined as such:

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

Here is how the date picker is displayed:

enter image description here

How can I get the date (4/22/2015) to center vertically in the textbox (the dotted line around the date is the actual textbox boundary)?

I have tried setting both VerticalContentAlignment and VerticalAlignment to Center but this doesn't affect the date centering.

If I dig into the date picker using Snoops I can see an element called PART_TextBox within the DatePicker. If I change this PART_TextBox VerticalContentAlignment to Center, the text will be centered (is set to Stretch by default). However, I do not know how to access this subcomponent to change it's VerticalContentAlignment

like image 642
BrianKE Avatar asked Apr 01 '15 16:04

BrianKE


1 Answers

I would guess this is a product of your font size. Try.

<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>
like image 176
Andy Reed Avatar answered Nov 05 '22 09:11

Andy Reed