WPF工具包日历控件如何绑定BlackoutDates?我想将日期列表绑定到BlackoutDates属性,但这似乎不可能。特别是在MVVM场景中。以前有人做过这样的事吗?是否有任何与MVVM配合良好的日历控件?对于你的DatePicker困境,我发现了一个使用附加属性的巧妙技巧(根据我对CommandBindings的使用进行了修改):不可变的,不能以其他方式绑定。////用法:publicstaticDependencyPropertyRegisterBlackoutDatesProperty=DependencyProperty.RegisterAttached("RegisterBlackoutDates",typeof(System.Windows.Controls.CalendarBlackoutDatesCollection),typeof(AttachedProperties)Meta(null,OnRegisterCommandBindingChanged));publicstaticvoidSetRegisterBlackoutDates(UIElementelement,System.Windows.Controls.CalendarBlackoutDatesCollectionvalue){if(element!=null)element.SetValue(RegisterBlackoutDatesProperty,value);}publicstaticSystem.Windows.Controls.CalendarBlackoutDatesCollectionGetRegisterBlackoutDates(UIElement元素){重新转(元素!=null?(System.Windows.Controls.CalendarBlackoutDatesCollection)element.GetValue(RegisterBlackoutDatesProperty):null);}privatestaticvoidOnRegisterCommandBindingChanged(DependencyObjectsender,DependencyPropertyChangedEventArgse){System.Windows.Controls.DatePickerSystem.Windows.Controls.DatePicker;如果(元素!=null){System.Windows.Controls.CalendarBlackoutDatesCollection绑定=e.NewValueasSystem.Windows.Controls.CalendarBlackoutDatesCollection;if(bindings!=null){element.BlackoutDates.Clear();foreach(绑定中的vardateRange){element.BlackoutDates.Add(dateRange);}}}}#endregion}我确定我来不及帮你了,但希望其他人会发现它有用这是马特答案的改进版本,它允许我们像任何普通的Observable集合一样使用BlackoutDates(无需创建每次我们想要更改BlackoutDates时都会创建一个新集合)。我们存储所有日历和日期移除器的列表,并在它们的标签内存储MVVM中使用的集合。如果需要,对此类的简单修改将允许使用ObservableCollection://将命令绑定的集合添加到日期选择器的现有BlackoutDates集合,因为这些集合是不可变的,不能以其他方式绑定。//用法:publicclassCalendarAttachedProperties:DependencyObject{#region属性privatestaticreadonlyList_calendars=newList();privatestaticreadonlyList_datePickers=newList();#endregion#region依赖属性publicstaticDependencyPropertyRegisterBlackoutDatesProperty=DependencyProperty.RegisterAttached("RegisterBlackoutDates",typeof(CalendarBlackoutDatesCollection),typeof(CalendarAttachedProperties),newPropertyMetadata(null,OnRegisterCommandBindingChanged));publicstaticvoidSetRegisterBlackoutDates(DependencyObjectd,CalendarBlackoutDatesCollection值){d.SetValue(RegisterBlackoutDatesProperty,值);}publicstaticCalendarBlackoutDatesCollectionGetRegisterBlackoutDates(DependencyObjectd){返回(CalendarBlackoutDatesCollection)d.GetValue(RegisterBlackoutDatesProperty);}#endregion#region事件处理程序privatestaticvoidCalendarBindings_CollectionChanged(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgse){CalendarBlackoutDatesCollectionblackoutDates=senderasCalendarBlackoutDatesCollection;日历calendar=_calendars.First(c=>c.Tag==blackoutDates);如果(e.Action==NotifyCollectionChangedAction.Add){foreach(CalendarDateRangedateRangeine.NewItems){calendar.BlackoutDates.Add(dateRange);}}}privatestaticvoidDatePickerBindings_CollectionChanged(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgse){CalendarBlackoutDatesCollectionblackoutDates=senderasCalendarBlackoutDatesCollection;DatePickerdatePicker=_datePickers.First(c=>c.Tag==blackoutDates);如果(e.Action==NotifyCollectionChangedAction.Add){foreach(CalendarDateRange日期e.NewItems中的范围){datePicker.BlackoutDates.Add(dateRange);}}}#endregion#region私有方法privatestaticvoidOnRegisterCommandBindingChanged(DependencyObjectsender,DependencyPropertyChangedEventArgse){Calendarcalendar=senderasCalendar;如果(日历!=null){CalendarBlackoutDatesCollectionbindings=e.NewValueasCalendarBlackoutDatesCollection;if(bindings!=null){if(!_calendars.Contains(calendar)){calendar.Tag=bindings;_calendars.Add(日历);}calendar.BlackoutDates.Clear();foreach(绑定中的vardateRange){calendar.BlackoutDates.Add(dateRange);}bindings.CollectionChanged+=CalendarBindings_CollectionChanged;}}else{DatePickerdatePicker=senderasDatePicker;if(datePicker!=null){CalendarBlackoutDatesCollection绑定=e.NewValueasCalendarBlackoutDatesCollection;if(bindings!=null){if(!_datePickers.Contains(datePicker)){datePicker.Tag=bindings;_datePickers.Add(日期选择器r);}datePicker.BlackoutDates.Clear();foreach(绑定中的vardateRange){datePicker.BlackoutDates.Add(dateRange);}bindings.CollectionChanged+=DatePickerBindings_CollectionChanged;}}}}#endregion}这是ObservableCollection版本://将命令绑定的集合添加到日期选择器的现有BlackoutDates集合,因为这些集合是不可变的,不能以其他方式绑定。//用法:publicclassCalendarAttachedProperties:DependencyObject{#region属性privatestaticreadonlyList_calendars=newList();privatestaticreadonlyList_datePickers=newList();#endregion#region依赖属性publicstaticDependencyPropertyRegisterBlackoutDatesProperty=DependencyProperty.RegisterAttached("RegisterBlackoutDates",typeof(ObservableCollection),typeof(CalendarAttachedProperties),newPropertyMetadata(null,OnRegisterCommandBindingChanged));publicstaticvoidSetRegisterBlackoutDates(DependencyObjectd,ObservableCollection值){d.SetValue(RegisterBlackoutDatesProperty,值);}publicstaticObservableCollectionGetRegisterBlackoutDates(DependencyObjectd){return(ObservableCollection)d.GetValue(RegisterBlackoutDatesProperty);}#endregion#region事件处理程序privatestaticvoidCalendarBindings_CollectionChanged(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgse){ObservableCollectionblackoutDates=senderasObservableCollection;日历calendar=_calendars.First(c=>c.Tag==blackoutDates);如果(e.Action==NotifyCollectionChangedAction.Add){foreach(DateTimedateine.NewItems){calendar.BlackoutDates.Add(newCalendarDateRange(date));}}}privatestaticvoidDatePickerBindings_CollectionChanged(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgse){ObservableCollectionblackoutDates=senderasObservableCollection;DatePickerdatePicker=_datePickers.First(c=>c.Tag==blackoutDates);如果(e.Action==NotifyCollectionChangedAction.Add){foreach(DateTimedateine.NewItems){datePicker.BlackoutDates.Add(newCalendarDateRange(date));}}}#endregion#region私有方法privatestaticvoidOnRegisterCommandBindingChanged(DependencyObjectsender,DependencyPropertyChangedEventArgse){Calendarcalendar=senderasCalendar;if(calendar!=null){ObservableCollectionbindings=e.NewValueasObservableCollection;if(bindings!=null){if(!_calendars.Contains(calendar)){calendar.Tag=bindings;_calendars.Add(日历);}calendar.BlackoutDates.Clear();foreach(绑定中的DateTime日期){calendar.BlackoutDates.Add(newCalendarDateRange(date));}bindings.CollectionChanged+=CalendarBindings_CollectionChanged;}}else{DatePickerdatePicker=senderasDatePicker;if(datePicker!=null){ObservableCollection绑定=e.NewValueasObservableCollection;如果(绑定!=null){如果(!_datePickers.Contains(datePicker)){datePicker.Tag=绑定;_datePickers.Add(datePicker);}datePicker.BlackoutDates.Clear();foreach(绑定中的DateTime日期){datePicker.BlackoutDates.Add(newCalendarDateRange(date));}bindings.CollectionChanged+=DatePickerBindings_CollectionChanged;}}}}#endregion}我发现上面的例子(AttachedProperties类)我在我的Viewmodel中创建了一个这样的属性:publicCalendarBlackoutDatesCollectionBlackoutDates{get{return_blackoutDates;}设置{_blackoutDates=值;this.RaisePropertyChanged(p=>p.BlackoutDates);使用System.Collections.Generic从ObservableBase进行ViewModel检测;使用System.Linq;使用系统文本;使用System.ComponentModel;使用System.Windows.Data;使用系统集合;namespaceMySolution{公共抽象类ObservableBase:INotifyPropertyChanged{公共事件PropertyChangedEventHandlerPropertyChanged;publicvoidRaisePropertyChanged(stringpropertyName){if(this.PropertyChanged!=null){this.PropertyChanged(this,newPropertyChangedEventArgs(propertyName));这是窗口中使用此属性的Xaml:现在,当我想将BlackoutDates添加到日历时,我在ViewModel中调用UpdateCalendarBlackoutDates:2010,12,9),newDateTime(2010,12,9));CalendarDateRanger2=newCalendarDateRange(newDateTime(2010,12,10),newDateTime(2010,12,10));//因为我们无法从viewmodel获取真实的日历,并且我们无法在不为构造函数指定日历的情况下创建//新的CalendarBlackoutDatesCollection,我们提供了一个“虚拟日历”,只是为了满足//CalendarBlackoutDatesCollection...//因为你不能这样做:BlackoutDates=newCalendarBlackoutDatesCollection()。日历dummyCal=newCalendar();BlackoutDates=newCalendarBlackoutDatesCollection(dummyCal);//将日期范围添加到BlackOutDates属性BlackoutDates.Add(r);BlackoutDates.Add(r2);}这对我来说非常完美它可以通过更改OnRegisterCommandBindingChanged方法以接受DateRanges列表而不是CalendarBlackoutDatesCollection并将属性更改为列表来进一步完善:publicListBlackoutDates{等。但这现在对我有用..上面是C#学习教程:如何在WPF工具包中创建日历在控件中绑定BlackoutDates?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
