xamlwinui-3winuicalendarview

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


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


Solution

  • 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().