wpfxaml

How to change the size of some elements of WPF DatePicker


I'm writing a C# WPF application.
In a window i put two DatePicker objects: I was able to enlarge the font of the calendar and enlarge the image icon.

I also need to enlarge the font of the two first lines of the calendar (month + year) and the row of the days of week (see the image ..).

enter image description here

I created a copy of the style for DatePicker control but I do not identify the element to change in it. I defined a new setter for CalendarDayButton, CalendarDayButton, CalendarItem to set font to 36px but I dont see the result.

How can achieve that, please?


Solution

  • Let me show you a way using the VisualTreeHelper.

    First, you need to have a way to search controls through the VisualTree.

    public static IEnumerable<T> FindChildren<T>(DependencyObject parent)
        where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
    
            if (child is T childAsT)
            {
                yield return childAsT;
            }
    
            foreach (T grandChild in FindChildren<T>(child))
            {
                yield return grandChild;
            }
        }
    }
    

    Then, in the DatePicker's CalendarOpened event handler, we can find the Popup and controls inside it. The Popup.Child is a Calender control, which has 50 TextBlocks inside it. The items with index 1 to 7 are the TextBlocks for the days of the week.

    private void DatePickerControl_CalendarOpened(object sender, RoutedEventArgs e)
    {
        if (FindChildren<Popup>(DatePickerControl).FirstOrDefault() is not Popup popup)
        {
            return;
        }
    
        double fontSize = 20;
    
        if (FindChildren<Button>(popup.Child)
            .Where(x => x.Name == "PART_HeaderButton")
            .FirstOrDefault() is Button headerButton)
        {
            headerButton.FontSize = fontSize;
        }
    
        if (FindChildren<TextBlock>(popup.Child) is IEnumerable<TextBlock> textBlocks)
        {
            int targetChildIndex = 1;
    
            for (int i = 0; i < 7; i++)
            {
                TextBlock dayOfTheWeedTextBlock = textBlocks.ElementAt(targetChildIndex);
                dayOfTheWeedTextBlock.FontSize = fontSize;
                var text = dayOfTheWeedTextBlock.Text;
                targetChildIndex++;
            }
        }
    
        if (FindChildren<Grid>(popup.Child)
            .Where(x => x.Name == "PART_YearView")
            .FirstOrDefault() is Grid yearViewGrid)
        {
            foreach (CalendarButton calendarButton in FindChildren<CalendarButton>(yearViewGrid))
            {
                calendarButton.FontSize = fontSize;
            }
        }
    
        if (FindChildren<Button>(popup.Child)
            .Where(x => x.Name == "PART_NextButton")
            .FirstOrDefault() is Button nextButton)
        {
            nextButton.RenderTransform = new ScaleTransform(1.5, 1.5);
            nextButton.RenderTransformOrigin = new Point(0.5, 0.5);
        }
    
    }