如何在WPF工具包日历控件中绑定BlackoutDates?
我想将日期列表绑定到 BlackoutDates 属性,但这似乎不太可能。尤其是在 MVVM 场景中。有人完成过这样的事情吗?有没有可以与 MVVM 配合良好的日历控件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于您的 DatePicker 困境,我发现了一个使用附加属性的巧妙技巧(根据我对 CommandBindings 的使用进行了修改):
我确信我来不及帮助您了,但希望其他人会发现它有用。
For your DatePicker dilemma, I found a neat hack using attached properties (modified from my use of CommandBindings):
I'm sure I'm too late to help you out, but hopefully someone else will find it useful.
这是 Matt 答案的改进版本,它允许我们像处理任何普通的 Observable 集合一样使用 BlackoutDates(您不需要每次想要更改 BlackoutDates 时都创建新的集合)。我们存储所有绑定的日历和日期选择器的列表,并在它们的标签内存储 MVVM 中使用的集合。对该类进行简单的修改将允许使用 ObservableCollection。如果需要:
这是 ObservableCollection版本:
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<DateTime> if needed:
Here is the ObservableCollection<DateTime> version:
我实现了上面的示例(AttachedProperties 类)。我在 Viewmodel 中创建了一个属性,如下所示:
This ViewModel inerits from ObservableBase:
这是窗口中使用此属性的 Xaml:
现在,当我想将 BlackoutDates 添加到日历时,我在 ViewModel 中调用 UpdateCalendarBlackoutDates:
这非常适合我。通过更改 OnRegisterCommandBindingChanged 方法以接受 DateRanges 列表而不是 CalendarBlackoutDatesCollection,并将属性更改为这样的列表,可以进一步完善它:
但现在这对我有用。
I implemented the above example (the AttachedProperties class). I created a property in my Viewmodel like this:
This ViewModel inerits from ObservableBase:
This is the Xaml in the window that uses this property:
Now when I want to add BlackoutDates to the calendar, I call UpdateCalendarBlackoutDates in my ViewModel:
This works perfectly for me. It could be further perfected by changing the OnRegisterCommandBindingChanged method to accept a List of DateRanges instead of a CalendarBlackoutDatesCollection, and changing the property to a List like this:
but for now this works for me..