我想绑定一个日期列表到BlackoutDates属性,但它似乎是不可能的.特别是在MVVM场景中.有没有人完成这样的事情?有没有任何良好的日历控件,使MVVM播放很好?
解决方法
对于您的DatePicker困境,我发现使用附加属性(从我使用CommandBindings修改)的整洁的黑客:
class AttachedProperties : DependencyObject
{
#region RegisterBlackoutDates
// Adds a collection of command bindings to a date picker's existing BlackoutDates collection,since the collections are immutable and can't be bound to otherwise.
//
// Usage: <DatePicker hacks:AttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}" >
public static DependencyProperty RegisterBlackoutDatesProperty = DependencyProperty.Registerattached("RegisterBlackoutDates",typeof(System.Windows.Controls.CalendarBlackoutDatesCollection),typeof(AttachedProperties),new PropertyMetadata(null,OnRegisterCommandBindingChanged));
public static void SetRegisterBlackoutDates(UIElement element,System.Windows.Controls.CalendarBlackoutDatesCollection value)
{
if (element != null)
element.SetValue(RegisterBlackoutDatesProperty,value);
}
public static System.Windows.Controls.CalendarBlackoutDatesCollection GetRegisterBlackoutDates(UIElement element)
{
return (element != null ? (System.Windows.Controls.CalendarBlackoutDatesCollection)element.GetValue(RegisterBlackoutDatesProperty) : null);
}
private static void OnRegisterCommandBindingChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
{
System.Windows.Controls.DatePicker element = sender as System.Windows.Controls.DatePicker;
if (element != null)
{
System.Windows.Controls.CalendarBlackoutDatesCollection bindings = e.NewValue as System.Windows.Controls.CalendarBlackoutDatesCollection;
if (bindings != null)
{
element.BlackoutDates.Clear();
foreach (var daterange in bindings)
{
element.BlackoutDates.Add(daterange);
}
}
}
}
#endregion
}
我相信我太晚了,不能帮助你,但希望别人会觉得有用.