我尝试使用 calendarviewdayitemchanging 事件,args.item.foreground=green 但这不起作用。
你可以这样试试:
<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:您还可以使用 CommunityToolkit 中的 Framework Extensions 而不是 GetTextBlock()。