我正在尝试更改wpf日历控件中给定日期的背景色
给定的日期在
但是当我尝试选择日期并更改颜色时,它会更改每个月该天的颜色,而不是我需要更改该月该日期的颜色
HashSet突出显示日期
目前看起来
2023-08-24 00:00:00
2023-08-28 00:00:00
2023-08-29 00:00:00
2023-08-30 00:00:00
我尝试根据以下代码更改颜色:
private void PrintAllCalendarDates(DependencyObject depObj, HashSet<DateTime> highlightedDates)
{
for (int month = 1; month <= 12; month++)
{
// Set the displayed month in the calendar
customCalendar.DisplayDate = new DateTime(customCalendar.DisplayDate.Year, month, 1);
// Wait for the calendar to update (you may need to handle the Loaded event)
customCalendar.UpdateLayout();
// Get the PART_MonthView for the current month
DependencyObject monthView = GetMonthView(depObj, month);
if (monthView == null)
{
// Month view not found, continue to the next month
continue;
}
// This is a CalendarButton for a specific month, so let's get its CalendarDayButtons
int monthCalendarChildrenCount = VisualTreeHelper.GetChildrenCount(monthView);
for (int j = 0; j < monthCalendarChildrenCount; j++)
{
DependencyObject monthChild = VisualTreeHelper.GetChild(monthView, j);
if (monthChild is CalendarDayButton calendarDayButton)
{
foreach (DateTime datehash in highlightedDates)
{
// You can access each date in the HashSet here
//Debug.WriteLine(datehash.ToString());
}
//if (highlightedDates.Any(d => d.Year == date.Year && d.Month == date.Month && d.Day == date.Day))
//{
// // Change the background color to red
// calendarDayButton.Background = Brushes.Red;
//}
DateTime date = (DateTime)calendarDayButton.DataContext;
//if (highlightedDates.Contains(date.Date))
//{
// foreach (var highlightedDate in highlightedDates)
// {
// //if (date.Month == highlightedDate.Month && date.Year == highlightedDate.Year && date.Day == highlightedDate.Day)
// //{
// // // Change the background color to red
// // Debug.WriteLine(date.ToString());
// // calendarDayButton.Background = Brushes.Red;
// //}
// if (date.Month == 8 && date.Year == 2023 && highlightedDates.Contains(date.Date))
// {
// // Change the background color to red
// Debug.WriteLine(date.ToString());
// calendarDayButton.Background = Brushes.Red;
// }
// }
//}
if (date.Month == 8 && date.Year == 2023)
{
if (highlightedDates.Contains(date.Date))
{
// Change the background color to red
Debug.WriteLine(date.ToString());
calendarDayButton.Background = Brushes.Red;
}
else
{
// Set the background color to its default value for dates in August
calendarDayButton.Background = Brushes.White; // or any other default color
}
}
else
{
// Set the background color to its default value for dates in other months
calendarDayButton.Background = Brushes.White; // or any other default color
}
}
}
}
}
private DependencyObject GetMonthView(DependencyObject depObj, int targetMonth)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child is FrameworkElement frameworkElement && frameworkElement.Name == "PART_MonthView")
{
// Check if this month matches the target month
int month = customCalendar.DisplayDate.Month;
if (month == targetMonth)
{
return child;
}
}
// Recursively call the method to check child elements
DependencyObject result = GetMonthView(child, targetMonth);
if (result != null)
{
return result; // Found the target month view
}
}
return null; // Month view not found
}
if语句工作正常,但不是只挂起月份8的背景颜色,而是更改了日历中所有月份的正确日期。
下面是calendar控件的xaml以获取更多上下文:
<Calendar x:Name="customCalendar" DisplayDate="{Binding CurrentDate}" Margin="-533,0,533,823" Grid.RowSpan="5">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton">
<Setter Property="Background" Value="Transparent" />
</Style>
</Calendar.CalendarDayButtonStyle>
</Calendar>
1条答案
按热度按时间ppcbkaq51#
CalendarDayButton
元素被重用。你应该使用一个转换器和一个MultiBinding
来设置Background
的颜色,而不是通过编程方式直接修改视觉元素:XAML:
窗口/视图模型: