none
ArgumentException bei AttachedProperty in XAML

    Allgemeine Diskussion

  • Hallo zusammen,

    ich habe ein Problem mit einem AttachedProperty in XAML. Zunächst zum Aufbau:

    1) Die Klasse, welches das Property "MySelectedDates" beinhaltet

        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);
            }
    }

    2) Das Binding in XAML, welches das Problem verursacht:

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

    3) Das ViewModel, welches im Kalender benutzt wird:

        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>();
            }
        }

    Das Binding im XAML verursacht den folgenden Fehler:

    Error 5 The value "System.Windows.Data.Binding" is not of type "System.DateTime" and cannot be used in this generic collection.

    Für Ideen und Anregungen wäre ich dankbar :)!

    Viele Grüße,

    Nico

    Mittwoch, 17. Juli 2013 08:29

Alle Antworten