I'm using a DatePicker in WPF and would like to vertically align the text to the center of the DatePicker.
Is there any way to do this, short of writing my own DatePicker? Thanks in advance.
Try this in XAML add style to DatePickerTextBox of DatePicker it should work.
<DatePicker Width="200" VerticalAlignment="Center" Height="45" Background="White"
FontSize="8" VerticalContentAlignment="Center">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DatePicker.Resources>
</DatePicker>
Personally I think it looks better if you keep the font size to 12 and apply a scale to the DatePickers LayoutTransform, while my second choice would be to template out the whole thing. To answer your question though, a quick and easy solution is to add a Loaded handler in the parent window/control's constructor:
public MainWindow()
{
InitializeComponent();
theDatePicker.Loaded += TheDatePicker_Loaded;
}
And then override the button's vertical alignment in the handler:
private void TheDatePicker_Loaded(object sender, RoutedEventArgs e)
{
var button = theDatePicker.Template.FindName("PART_Button", theDatePicker) as Button;
button.VerticalAlignment = VerticalAlignment.Center;
}
FindName is returning anything at all prior to casting it to type Button, could be that whatever GUI framework you're using has replaced it with something else.
WPF DatePickerdate in textbox is already vertically aligned . what do you want ?DatePicker.Text.VerticalAlignment = VerticalAlignment.Center?