Putting Content in MonthCalendar "Days"

Editors for WPF Forum

Posted 12 years ago by Josh Luth - Software Developer, Esha Research
Version: 11.2.0553
Avatar

I was looking into using the MonthCalendar control as a way to host content on certain days, not just change the colors. Is there a way to replace the template of the Days and bind them (MVVM style) to my content? From what I can tell, the DayItems content is just text for the date.

Comments (8)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Josh,

You could certainly customize the content in the DayItem elements, which are used to present the days of the month. You would use an implicit Style that changes the Content and/or ControlTemplate.

The MVVM aspect would be a bit different, as this is not like an ItemsControl where the view models have containers built for them. Instead, you would need to lookup a given DateTime and determine it's content based on that. You would probably need to use an attached behavior to accomplish this.


Actipro Software Support

Posted 12 years ago by Josh Luth - Software Developer, Esha Research
Avatar

Say I just want to keep it simple and change the Background color of one of the DayItems if my ViewModel has data for that Date. How would you suggest I go about that. For some reason its not clear to me how to approach it since this isn't an itemscontrol.

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Josh,

You would need to use a DayItemStyleSelector as shown in our MonthCalendar Custom Styles QuickStart. Since your view model would be contained in the MonthCalendar.DataContext, your selector would need to query that based on the current DayItem's date.

Unlike an ItemsControl, the "items" in the MonthCalendar are automatically generated. But you can still apply Styles and DataTemplates in generally the same manner.


Actipro Software Support

Posted 12 years ago by Josh Luth - Software Developer, Esha Research
Avatar

I'm just now getting back to this issue. I've started down the road of using a StyleSelector, but I ran into a problem. I have my DayItem in the selector, but the MonthCalendar property and the DataContext property of the DayItem are both null, so how do I get to my vm?

Posted 12 years ago by Rory Plaire
Avatar

I've also looked into this issue, and it's confusing why the DayItem doesn't have the DataContext inherited from the MonthCalendar. As far as I can tell, the DayItem is generated in a MonthView, which is used by a TransitionPresenter internal to MonthCalendar to display a grid of DayItem instances. It appears that the way that the TransitionPresenter creates the views somehow causes it to have a null DataContext. Is this expected? If so, it appears we should be binding the DataContext to the individual ViewBase instances in the MonthCalendar.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Josh,

I debugged it and the problem is that the style selector is called as the view is being created.  Due to the use of ContentPresenters between MonthCalendar and the DayItem, some of the visuals haven't yet been realized at the time the style selector runs.  That's why DayItem.MonthCalendar is null.  Also there are some places where the logical tree doesn't bubble up to the MonthCalendar, so DataContext doesn't inherit down.

Could you possibly set a property on your style selector instead to track the data context?


Actipro Software Support

Posted 12 years ago by Josh Luth - Software Developer, Esha Research
Avatar

You can't bind the data context to a property on the StyleSelector because the StyleSelector isn't a DependencyObject. Unless there is something I'm not thinking of.

Posted 12 years ago by Josh Luth - Software Developer, Esha Research
Avatar

Well I went a bit of a different way but got it to work. I ended up binding the DayItemStyleSelector to a property in my View Model, which returns my CustomStyleSelector that has been initialized with the data that I need, in this case a Func pointing back to my view model.

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.