How can we change foreground of dayitem (1,2,3..) of calendarview?

61 Views Asked by At

I have tried using the calendarviewdayitemchanging event , args.item.foreground=green But this is not working.

1

There are 1 best solutions below

1
Andrew KeepCoding On BEST ANSWER

You can try this way:

<CalendarView CalendarViewDayItemChanging="CalendarView_CalendarViewDayItemChanging" />
private void CalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
    if (args.Item.Date.DayOfWeek is DayOfWeek.Saturday &&
        GetTextBlock(args.Item) is TextBlock saturdayTextBlock)
    {
        saturdayTextBlock.Foreground = new SolidColorBrush(Colors.SkyBlue);
    }
}

private TextBlock? GetTextBlock(CalendarViewDayItem calendarViewDayItem)
{
    int childrenCount = VisualTreeHelper.GetChildrenCount(calendarViewDayItem);

    for (int i = 0; i < childrenCount; i++)
    {
        if (VisualTreeHelper.GetChild(calendarViewDayItem, i) is TextBlock textBlock)
        {
            return textBlock;
        }
    }

    return null;
}

PS: You also can use Framework Extensions from the CommunityToolkit instead of GetTextBlock().