xaml - How to style the BlackoutDates from WPF calendar -
i want style the blackoutdates of wpf calendar. default visualisation gray cross above date numeral. how gray them out example?
here go
<calendar> <calendar.calendardaybuttonstyle> <style targettype="calendardaybutton" basedon="{staticresource {x:type calendardaybutton}}"> <style.triggers> <trigger property="isblackedout" value="true"> <setter property="background" value="lightgray"/> </trigger> </style.triggers> </style> </calendar.calendardaybuttonstyle> <calendar.blackoutdates> <calendardaterange start="24-june-2014" end="25-june-2014"/> </calendar.blackoutdates> </calendar>
i have specified custom style calendardaybuttonstyle
, in style defined trigger on isblackedout
, set background
lightgray
result
you can choose manipulate other properties per needs
remove existing blackout
to remove existing blackout have options of defining custom template calendar day button, drawback you'll have maintain every theme eg luna, classic etc
i going propose solution utilize existing template no need worry such issues
i used attached properties same
class calenderhelper
namespace csharpwpf { class calenderhelper : dependencyobject { public static bool getisblackoutdisabled(dependencyobject obj) { return (bool)obj.getvalue(isblackoutdisabledproperty); } public static void setisblackoutdisabled(dependencyobject obj, bool value) { obj.setvalue(isblackoutdisabledproperty, value); } // using dependencyproperty backing store isblackoutdisabled. enables animation, styling, binding, etc... public static readonly dependencyproperty isblackoutdisabledproperty = dependencyproperty.registerattached("isblackoutdisabled", typeof(bool), typeof(calenderhelper), new propertymetadata(false, onisblackoutdisabledchanged)); private static void onisblackoutdisabledchanged(dependencyobject d, dependencypropertychangedeventargs e) { calendardaybutton daybutton = d calendardaybutton; if (daybutton.isloaded) { setblackout(daybutton, (bool)e.newvalue); } else { daybutton.loaded += (s, ee) => { setblackout(daybutton, (bool)e.newvalue); }; } } static void setblackout(calendardaybutton daybutton, bool collapsed) { controltemplate template = daybutton.template; path blackoutpath = template.findname("blackout", daybutton) path; if (collapsed) blackoutpath.visibility = system.windows.visibility.collapsed; else blackoutpath.visibility = system.windows.visibility.visible; } } }
i have created class calenderhelper
attached property isblackoutdisabled
hide blackout element in existing template
xaml
<calendar x:name="cal" xmlns:l="clr-namespace:csharpwpf"> <calendar.calendardaybuttonstyle> <style targettype="calendardaybutton" basedon="{staticresource {x:type calendardaybutton}}"> <style.triggers> <trigger property="isblackedout" value="true"> <setter property="background" value="lightgray"/> <setter property="l:calenderhelper.isblackoutdisabled" value="true"/> </trigger> </style.triggers> </style> </calendar.calendardaybuttonstyle> <calendar.blackoutdates> <calendardaterange start="24-june-2014" end="25-june-2014"/> </calendar.blackoutdates> </calendar>
i have enabled newly created property l:calenderhelper.isblackoutdisabled
in trigger, in turn hide default blackout visual
result
this approach make flexible many purpose, may code other sophisticate functions not doable via simple xaml
Comments
Post a Comment