tags:

views:

394

answers:

2

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
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
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
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