1

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.

Edit: To visualize my poblem:

5
  • in WPF DatePicker date in textbox is already vertically aligned . what do you want ? Commented Apr 6, 2020 at 9:37
  • i want to align the text inside the textbox to the center of the textbox Commented Apr 6, 2020 at 10:38
  • this doesn't work DatePicker.Text.VerticalAlignment = VerticalAlignment.Center ? Commented Apr 6, 2020 at 11:26
  • Maybe we are talking about different DatePicker? I'm using the System.Windows.Controls.DatePicker. This has .Text, but that has no VerticalAlignment. Commented Apr 6, 2020 at 11:31
  • Check my answer please ! Commented Apr 6, 2020 at 11:36

2 Answers 2

0

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>
Sign up to request clarification or add additional context in comments.

4 Comments

This soltuion will always work if you play with height or font property even.
Yes i tried and then suggested you this because Editing TEMPLATE is not the most excellent way of making small changes. Edit Template only in case when you want to alter the style of an element.
Got it. Thanks. I definitley learned some useful things today.
Good to hear :)
0

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;
}

8 Comments

unfortunatly 'button' is null. Maybe because i don't define any templates? Or maybe "PART_Button" is wrong?
You are doing it in the Loaded handler and not the constructor, right? If so that would suggest that something has overloaded the default template and removed PART_Button ('s definitely part of the standard WPF template for both Core and Standard). Try putting the mouse cursor on the DatePicker in the XAML, then go to properties, click the little square to the right of Miscellaneous -> Template and select "Convert to Local Value". That will automatically template out the control for you and remove any other template that's being applied. You should also see PART_Button in that template.
One other thing....check whether 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.
FindName already returns null; But the Template shows up like you said and it has "PART_Button". How can i find out, if and where the template is replaced?
Also, now that the code of the template is visible, i get an error: Index 0 is out of range of the PathParameters list, which has length 0. NotesSetup.xaml 141 The program still runs though. I also tried changing the value directly in the template (setting verticalAlignment od PART_Button to center) . It didn't change anything
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.