Hi,
I'm trying to use the CalendarDayStyleSelector in the XamMonthCalendar control. i want to highlight the dates that are in a observable collection in my view model. i've written a style selector and this is referenced using
<UserControl.Resources>
<local:DateStyleSelector x:Key="dayStyle" />
</UserControl.Resources>
...
CalendarDayStyleSelector="{DynamicResource dayStyle}"
and in the code behind
Style dayStyle = new Style(typeof(Control));
var date = (CalendarDay) item;
var viewModel = (HolidaysViewModel) date.DataContext;
if (viewModel.DatesForSelectedFinCentres.Contains(date.StartDate))
{
dayStyle.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Wheat));
}
this works when you change month in the calendar as the styles are reset.
is there a way to make the styleselector refresh when the observable collection changes
many thanks
There's no relation between the StyleSelector instance that the control has a reference to and whatever is used in the code behind so no there isn't anything that would cause changing the collection you're using to affect the view. You would likely have to do something to cause the StyleSelector property to be changed - e.g. since you're using a DynamicResource you could add and remove an instance of a StyleSelector from the control's resources with the same key.
how would i add and remove an instance at runtime?
am happy to hear other suggestions for how to do this - i've looked at converters and didn't get anywhere with them either. this seems to be the closest i've got(but not quite there!). Every example on the net for people trying to do this(same for the wpf calendar control) is when they have a static list and the dates are highlighted when the control is first loaded
This is basically just how StyleSelectors work in WPF. With regards to adding/removing an instance I meant having a reference to the control and in code creating a StyleSelector and adding to the Resources of the xamMonthCalendar (or something else between it and the UserControl where you have the StyleSelector) using the same key that you're using and then removing it. Alternatively you could try binding the CalendarDayStyleSelector property to a property on your VM. Then when the collection changes you would set the property on your VM to a new instance of your DateStyleSelector.
bingo! got it working. did exactly that. the vm now has public property of the styleselector. everytime the list refreshes, the style selector is reinstantiated. In the wpf, the style selector is bound to the public property. easy:)
thanks for your help
jondando,
Would you be able to share the VM code with me, please?