如何在 WPF Toolkit 日历控件中绑定 BlackoutDates? [英] How to Bind BlackoutDates in WPF Toolkit Calendar control?
本文介绍了如何在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文