Hi all together!
I have a problem with an AttachedProperty in XAML that throws an exception in the designer. Because of not being able to change the design because of that I really want to get rid of that error.
The error is the following:
Error 5 The value "System.Windows.Data.Binding" is not of type "System.DateTime" and cannot be used in this generic collection.
The binding that causes the error is the attached property "local:SelectedDatesBehavior.MySelectedDates" in the following snippet:
<Calendar DataContext="{Binding Path=CalendarViewModels[2]}" DisplayDate="{Binding Path=DisplayDate}" Style="{StaticResource styleDatePickerCalendar}" DisplayDateStart="{Binding Path=StartDate}" DisplayDateEnd="{Binding Path=EndDate}" local:SelectedDatesBehavior.MySelectedDates="{Binding Path=SelectedDates}"></Calendar>
The ViewModel used for the calendar is the following:
public class CalendarViewModel : DependencyObject { public DateTime StartDate { get { return (DateTime)this.GetValue(StartDateProperty); } set { this.SetValue(StartDateProperty, value); } } public static readonly DependencyProperty StartDateProperty = DependencyProperty.Register("StartDate", typeof(DateTime), typeof(CalendarViewModel)); public DateTime EndDate { get { return (DateTime)this.GetValue(EndDateProperty); } set { this.SetValue(EndDateProperty, value); } } public static readonly DependencyProperty EndDateProperty = DependencyProperty.Register("EndDate", typeof(DateTime), typeof(CalendarViewModel)); public DateTime DisplayDate { get { return (DateTime)this.GetValue(DisplayDateProperty); } set { this.SetValue(DisplayDateProperty, value); } } public static readonly DependencyProperty DisplayDateProperty = DependencyProperty.Register("DisplayDate", typeof(DateTime), typeof(CalendarViewModel)); public bool IsTodayHighlighted { get { return (bool)this.GetValue(IsTodayHighlightedProperty); } set { this.SetValue(IsTodayHighlightedProperty, value); } } public static readonly DependencyProperty IsTodayHighlightedProperty = DependencyProperty.Register("IsTodayHighlighted", typeof(bool), typeof(CalendarViewModel), new UIPropertyMetadata(true)); public ObservableCollection<DateTime> SelectedDates { get { return (ObservableCollection<DateTime>)this.GetValue(SelectedDatesProperty); } protected set { this.SetValue(SelectedDatesProperty, value); } } public static readonly DependencyProperty SelectedDatesProperty = DependencyProperty.Register("SelectedDates", typeof(ObservableCollection<DateTime>), typeof(CalendarViewModel)); public CalendarViewModel() { this.SelectedDates = new ObservableCollection<DateTime>(); } public void SetDates(IEnumerable<DateTime> dates) { ObservableCollection<DateTime> newDates = new ObservableCollection<DateTime>(); foreach (DateTime item in dates) { newDates.Add(item); } this.SelectedDates = newDates; } public void ClearDates() { this.SelectedDates = new ObservableCollection<DateTime>(); } }
And the static class with the logic for the attached property is this:
public class SelectedDatesBehavior { public static readonly DependencyProperty MySelectedDatesProperty = DependencyProperty.RegisterAttached("MySelectedDates", typeof(ObservableCollection<DateTime>), typeof(SelectedDatesBehavior), new FrameworkPropertyMetadata(OnSelectedDatesPropertyChanged)); public static void SetMySelectedDates(UIElement calendar, ObservableCollection<DateTime> value) { calendar.SetValue(MySelectedDatesProperty, value); } public static ObservableCollection<DateTime> GetMySelectedDates(UIElement calendar) { return (ObservableCollection<DateTime>)calendar.GetValue(MySelectedDatesProperty); } public static ObservableCollection<DateTime> GetMySelectedDates(Calendar calendar) { return (ObservableCollection<DateTime>)calendar.GetValue(MySelectedDatesProperty); } }
Feel free to ask further questions if the structure is not clear. I'm thankful for every hint that could lead to solve the problem :)!
Best wishes,
Nico