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?
+1
A:
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.
mattdekrey
2010-07-07 04:47:00
No that project was put on hold for the time being. I'm not sure I understand Attached Properties yet, but I thought that was a way to do it, I just didn't know how at the time. Time to get back on the WPF bandwagon.
nportelli
2010-07-07 14:05:51
Yeah, I'm not 100% certain I 'get' Attached Properties yet - but it seems to have two uses: 1. An alternative to a Dynamic-Object-keyed dictionary for additional data (like the Canvas object), or 2. an extension-method-like use from XAML. Hmm, a community wiki on the subject could be really interesting.
mattdekrey
2010-07-07 14:14:16
A:
Can I have a simple example of how to use this in XAML please ? Im using it in an MVVM scenario. I have a list of dates in my View Model which I want to black out on calender by data binding.
Thank you
mary
2010-10-22 15:55:13