Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an improved version of Matt's answer that allows us to work with the BlackoutDates as with any normal Observable collection (you don't need to create new collections each time you want to change the BlackoutDates). We store a list of all the calendars and datepickers binded and inside their tag we store the collection used in MVVM. An easy modification of the class will allow to work with ObservableCollection&lt;DateTime&gt; if needed:</p> <pre><code>// 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: &lt;DatePicker CalendarAttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}" &gt; public class CalendarAttachedProperties : DependencyObject { #region Attributes private static readonly List&lt;Calendar&gt; _calendars = new List&lt;Calendar&gt;(); private static readonly List&lt;DatePicker&gt; _datePickers = new List&lt;DatePicker&gt;(); #endregion #region Dependency Properties public static DependencyProperty RegisterBlackoutDatesProperty = DependencyProperty.RegisterAttached("RegisterBlackoutDates", typeof(CalendarBlackoutDatesCollection), typeof(CalendarAttachedProperties), new PropertyMetadata(null, OnRegisterCommandBindingChanged)); public static void SetRegisterBlackoutDates(DependencyObject d, CalendarBlackoutDatesCollection value) { d.SetValue(RegisterBlackoutDatesProperty, value); } public static CalendarBlackoutDatesCollection GetRegisterBlackoutDates(DependencyObject d) { return (CalendarBlackoutDatesCollection)d.GetValue(RegisterBlackoutDatesProperty); } #endregion #region Event Handlers private static void CalendarBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { CalendarBlackoutDatesCollection blackoutDates = sender as CalendarBlackoutDatesCollection; Calendar calendar = _calendars.First(c =&gt; c.Tag == blackoutDates); if (e.Action == NotifyCollectionChangedAction.Add) { foreach (CalendarDateRange dateRange in e.NewItems) { calendar.BlackoutDates.Add(dateRange); } } } private static void DatePickerBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { CalendarBlackoutDatesCollection blackoutDates = sender as CalendarBlackoutDatesCollection; DatePicker datePicker = _datePickers.First(c =&gt; c.Tag == blackoutDates); if (e.Action == NotifyCollectionChangedAction.Add) { foreach (CalendarDateRange dateRange in e.NewItems) { datePicker.BlackoutDates.Add(dateRange); } } } #endregion #region Private Methods private static void OnRegisterCommandBindingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { Calendar calendar = sender as Calendar; if (calendar != null) { CalendarBlackoutDatesCollection bindings = e.NewValue as CalendarBlackoutDatesCollection; if (bindings != null) { if (!_calendars.Contains(calendar)) { calendar.Tag = bindings; _calendars.Add(calendar); } calendar.BlackoutDates.Clear(); foreach (var dateRange in bindings) { calendar.BlackoutDates.Add(dateRange); } bindings.CollectionChanged += CalendarBindings_CollectionChanged; } } else { DatePicker datePicker = sender as DatePicker; if (datePicker != null) { CalendarBlackoutDatesCollection bindings = e.NewValue as CalendarBlackoutDatesCollection; if (bindings != null) { if (!_datePickers.Contains(datePicker)) { datePicker.Tag = bindings; _datePickers.Add(datePicker); } datePicker.BlackoutDates.Clear(); foreach (var dateRange in bindings) { datePicker.BlackoutDates.Add(dateRange); } bindings.CollectionChanged += DatePickerBindings_CollectionChanged; } } } } #endregion } </code></pre> <p>Here is the ObservableCollection&lt;DateTime&gt; version:</p> <pre><code>// 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: &lt;DatePicker hacks:AttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}" &gt; public class CalendarAttachedProperties : DependencyObject { #region Attributes private static readonly List&lt;Calendar&gt; _calendars = new List&lt;Calendar&gt;(); private static readonly List&lt;DatePicker&gt; _datePickers = new List&lt;DatePicker&gt;(); #endregion #region Dependency Properties public static DependencyProperty RegisterBlackoutDatesProperty = DependencyProperty.RegisterAttached("RegisterBlackoutDates", typeof(ObservableCollection&lt;DateTime&gt;), typeof(CalendarAttachedProperties), new PropertyMetadata(null, OnRegisterCommandBindingChanged)); public static void SetRegisterBlackoutDates(DependencyObject d, ObservableCollection&lt;DateTime&gt; value) { d.SetValue(RegisterBlackoutDatesProperty, value); } public static ObservableCollection&lt;DateTime&gt; GetRegisterBlackoutDates(DependencyObject d) { return (ObservableCollection&lt;DateTime&gt;)d.GetValue(RegisterBlackoutDatesProperty); } #endregion #region Event Handlers private static void CalendarBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { ObservableCollection&lt;DateTime&gt; blackoutDates = sender as ObservableCollection&lt;DateTime&gt;; Calendar calendar = _calendars.First(c =&gt; c.Tag == blackoutDates); if (e.Action == NotifyCollectionChangedAction.Add) { foreach (DateTime date in e.NewItems) { calendar.BlackoutDates.Add(new CalendarDateRange(date)); } } } private static void DatePickerBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { ObservableCollection&lt;DateTime&gt; blackoutDates = sender as ObservableCollection&lt;DateTime&gt;; DatePicker datePicker = _datePickers.First(c =&gt; c.Tag == blackoutDates); if (e.Action == NotifyCollectionChangedAction.Add) { foreach (DateTime date in e.NewItems) { datePicker.BlackoutDates.Add(new CalendarDateRange(date)); } } } #endregion #region Private Methods private static void OnRegisterCommandBindingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { Calendar calendar = sender as Calendar; if (calendar != null) { ObservableCollection&lt;DateTime&gt; bindings = e.NewValue as ObservableCollection&lt;DateTime&gt;; if (bindings != null) { if (!_calendars.Contains(calendar)) { calendar.Tag = bindings; _calendars.Add(calendar); } calendar.BlackoutDates.Clear(); foreach (DateTime date in bindings) { calendar.BlackoutDates.Add(new CalendarDateRange(date)); } bindings.CollectionChanged += CalendarBindings_CollectionChanged; } } else { DatePicker datePicker = sender as DatePicker; if (datePicker != null) { ObservableCollection&lt;DateTime&gt; bindings = e.NewValue as ObservableCollection&lt;DateTime&gt;; if (bindings != null) { if (!_datePickers.Contains(datePicker)) { datePicker.Tag = bindings; _datePickers.Add(datePicker); } datePicker.BlackoutDates.Clear(); foreach (DateTime date in bindings) { datePicker.BlackoutDates.Add(new CalendarDateRange(date)); } bindings.CollectionChanged += DatePickerBindings_CollectionChanged; } } } } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload