WPF: DatePicker text is centered vertically

I am using DatePicker in my application, defined as such:

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

The following shows how to select a date:

enter image description here

How can I get the date (4/22/2015) to center vertically in the text box (the dashed line around the date is the actual border of the text box)?

I tried setting both VerticalContentAlignment and VerticalAlignment to Center , but this does not affect the centering of the date.

If I write to a date picker using Snoops, I can see an element named PART_TextBox in DatePicker . If I change this PART_TextBox VerticalContentAlignment to Center , the text will be centered ( Stretch set by default). However, I do not know how to access this subcomponent to change its VerticalContentAlignment

+6
source share
1 answer

I would suggest that this is a product of your font size. Give it a 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> 
0
source

Source: https://habr.com/ru/post/984569/


All Articles