本文介绍了如何在WPF Toolkit日历控件中绑定BlackoutDates?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将日期列表绑定到BlackoutDates属性,但实际上似乎不可能.尤其是在MVVM场景中.有人完成过这样的事情吗?是否有任何可以很好地与MVVM配合使用的日历控件?
I'd like to bind a list of dates to the BlackoutDates property but it doesn't really seem to possible. Especially in a MVVM scenario. Has anyone accomplished something like this? Are there any good calendar controls that play nice with MVVM?
推荐答案
对于您的DatePicker困境,我发现了一个使用附加属性(从我对CommandBindings的使用进行修改而来)的简洁技巧:
For your DatePicker dilemma, I found a neat hack using attached properties (modified from my use of 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
}
我确定为时已晚,无法为您提供帮助,但希望其他人会发现它有用.
I'm sure I'm too late to help you out, but hopefully someone else will find it useful.
这篇关于如何在WPF Toolkit日历控件中绑定BlackoutDates?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!