终于找到了。为了繁荣而发帖,以防其他人试图做类似的事情。
正如评论中提到的,设置元素的填充颜色确实改变了背景颜色,但在所有的月份中,这一天都是这样。这实际上是有意义的,因为填充通常由模板管理,并基于绑定和转换器进行设置。
解决方案是在单击日期时重置鼠标向下事件上的绑定。
这是事件代码:
protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
if (!(e.OriginalSource is FrameworkElement &&
(e.OriginalSource as FrameworkElement).DataContext is DateTime))
{
base.OnPreviewMouseLeftButtonDown(e);
return;
}
DateTime dateTime = (DateTime)(e.OriginalSource as FrameworkElement).DataContext;
var calendarDay = _calendarDays.Single(d => d.CalendarDate == dateTime);
if (calendarDay.IsHoliday)
{
calendarDay.CalendarKey = null;
}
else
{
calendarDay.CalendarKey = Guid.NewGuid();
}
var holidayBackgroundRect = VisualTreeHelper.GetChild(VisualTreeHelper.GetParent(e.OriginalSource as DependencyObject), 1) as Rectangle;
var binding = new MultiBinding();
binding.Bindings.Add(new Binding());
binding.Bindings.Add(new Binding() { ElementName = "Calendar" });
binding.Converter = new CalendarDayColorConverter();
holidayBackgroundRect.SetBinding(Rectangle.FillProperty, binding);
base.OnPreviewMouseLeftButtonDown(e);
}
转换器决定了一天的颜色。它接受来自日历的当前绑定日期,以及日历控件本身,从中可以检索当前假日列表。
通过重新指定绑定,它强制日历日刷新该日期。
那是多么痛苦啊。