Displaying dates as holiday in WPF Calendar












0















I'm using WPF Calendar control to add Holidays. User can select days and the selected days will be inserted to a database table as holiday.
After the insertion, I'm displaying the days in Holiday table as BlackoutDates in the calendar as in the following code.



XAML



 <Calendar x:Name="HolidayCalendar" SelectedDatesChanged="Calendar_SelectionChanged"
SelectionMode="MultipleRange" Loaded="Calendar_Loaded">


C#



DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
GetHolidayService getHolidayService = new GetHolidayService();
ObservableCollection<GetHoliday> GetHolidayBaseList = new ObservableCollection<GetHoliday>();
PostHoliday postHoliday = new PostHoliday();
postHoliday.StartDate = startDate;
postHoliday.EndDate = endDate;
GetHolidayBaseList = getHolidayService.GetHolidayServiceFunction(postHoliday);
foreach (var item in GetHolidayList)
{

HolidayCalendar.BlackoutDates.Add(new CalendarDateRange(item.HolidayDate,item.HolidayDate)) ;
}


Current output



Current output



I want to achieve something like this - Holidays marked as Red



Preferred output



as in this link



Any help to write a control template required for this functionality is appreciated.



Thanks in advance!










share|improve this question

























  • Hey, I've edited my question. Will you please refer it again? Is my question clear now?

    – George T Kurian
    Nov 19 '18 at 15:02











  • I want to display Holidays as different, something like I mentioned in the picture (holidays marked as red). If I'm adding holidays to SelectedDates, if my user selects some new date, user can't differentiate SelectedDates from Holidays. That's why. Is it clear now?

    – George T Kurian
    Nov 19 '18 at 15:11
















0















I'm using WPF Calendar control to add Holidays. User can select days and the selected days will be inserted to a database table as holiday.
After the insertion, I'm displaying the days in Holiday table as BlackoutDates in the calendar as in the following code.



XAML



 <Calendar x:Name="HolidayCalendar" SelectedDatesChanged="Calendar_SelectionChanged"
SelectionMode="MultipleRange" Loaded="Calendar_Loaded">


C#



DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
GetHolidayService getHolidayService = new GetHolidayService();
ObservableCollection<GetHoliday> GetHolidayBaseList = new ObservableCollection<GetHoliday>();
PostHoliday postHoliday = new PostHoliday();
postHoliday.StartDate = startDate;
postHoliday.EndDate = endDate;
GetHolidayBaseList = getHolidayService.GetHolidayServiceFunction(postHoliday);
foreach (var item in GetHolidayList)
{

HolidayCalendar.BlackoutDates.Add(new CalendarDateRange(item.HolidayDate,item.HolidayDate)) ;
}


Current output



Current output



I want to achieve something like this - Holidays marked as Red



Preferred output



as in this link



Any help to write a control template required for this functionality is appreciated.



Thanks in advance!










share|improve this question

























  • Hey, I've edited my question. Will you please refer it again? Is my question clear now?

    – George T Kurian
    Nov 19 '18 at 15:02











  • I want to display Holidays as different, something like I mentioned in the picture (holidays marked as red). If I'm adding holidays to SelectedDates, if my user selects some new date, user can't differentiate SelectedDates from Holidays. That's why. Is it clear now?

    – George T Kurian
    Nov 19 '18 at 15:11














0












0








0








I'm using WPF Calendar control to add Holidays. User can select days and the selected days will be inserted to a database table as holiday.
After the insertion, I'm displaying the days in Holiday table as BlackoutDates in the calendar as in the following code.



XAML



 <Calendar x:Name="HolidayCalendar" SelectedDatesChanged="Calendar_SelectionChanged"
SelectionMode="MultipleRange" Loaded="Calendar_Loaded">


C#



DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
GetHolidayService getHolidayService = new GetHolidayService();
ObservableCollection<GetHoliday> GetHolidayBaseList = new ObservableCollection<GetHoliday>();
PostHoliday postHoliday = new PostHoliday();
postHoliday.StartDate = startDate;
postHoliday.EndDate = endDate;
GetHolidayBaseList = getHolidayService.GetHolidayServiceFunction(postHoliday);
foreach (var item in GetHolidayList)
{

HolidayCalendar.BlackoutDates.Add(new CalendarDateRange(item.HolidayDate,item.HolidayDate)) ;
}


Current output



Current output



I want to achieve something like this - Holidays marked as Red



Preferred output



as in this link



Any help to write a control template required for this functionality is appreciated.



Thanks in advance!










share|improve this question
















I'm using WPF Calendar control to add Holidays. User can select days and the selected days will be inserted to a database table as holiday.
After the insertion, I'm displaying the days in Holiday table as BlackoutDates in the calendar as in the following code.



XAML



 <Calendar x:Name="HolidayCalendar" SelectedDatesChanged="Calendar_SelectionChanged"
SelectionMode="MultipleRange" Loaded="Calendar_Loaded">


C#



DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
GetHolidayService getHolidayService = new GetHolidayService();
ObservableCollection<GetHoliday> GetHolidayBaseList = new ObservableCollection<GetHoliday>();
PostHoliday postHoliday = new PostHoliday();
postHoliday.StartDate = startDate;
postHoliday.EndDate = endDate;
GetHolidayBaseList = getHolidayService.GetHolidayServiceFunction(postHoliday);
foreach (var item in GetHolidayList)
{

HolidayCalendar.BlackoutDates.Add(new CalendarDateRange(item.HolidayDate,item.HolidayDate)) ;
}


Current output



Current output



I want to achieve something like this - Holidays marked as Red



Preferred output



as in this link



Any help to write a control template required for this functionality is appreciated.



Thanks in advance!







c# wpf wpf-controls






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 15:12







George T Kurian

















asked Nov 19 '18 at 8:49









George T KurianGeorge T Kurian

287




287













  • Hey, I've edited my question. Will you please refer it again? Is my question clear now?

    – George T Kurian
    Nov 19 '18 at 15:02











  • I want to display Holidays as different, something like I mentioned in the picture (holidays marked as red). If I'm adding holidays to SelectedDates, if my user selects some new date, user can't differentiate SelectedDates from Holidays. That's why. Is it clear now?

    – George T Kurian
    Nov 19 '18 at 15:11



















  • Hey, I've edited my question. Will you please refer it again? Is my question clear now?

    – George T Kurian
    Nov 19 '18 at 15:02











  • I want to display Holidays as different, something like I mentioned in the picture (holidays marked as red). If I'm adding holidays to SelectedDates, if my user selects some new date, user can't differentiate SelectedDates from Holidays. That's why. Is it clear now?

    – George T Kurian
    Nov 19 '18 at 15:11

















Hey, I've edited my question. Will you please refer it again? Is my question clear now?

– George T Kurian
Nov 19 '18 at 15:02





Hey, I've edited my question. Will you please refer it again? Is my question clear now?

– George T Kurian
Nov 19 '18 at 15:02













I want to display Holidays as different, something like I mentioned in the picture (holidays marked as red). If I'm adding holidays to SelectedDates, if my user selects some new date, user can't differentiate SelectedDates from Holidays. That's why. Is it clear now?

– George T Kurian
Nov 19 '18 at 15:11





I want to display Holidays as different, something like I mentioned in the picture (holidays marked as red). If I'm adding holidays to SelectedDates, if my user selects some new date, user can't differentiate SelectedDates from Holidays. That's why. Is it clear now?

– George T Kurian
Nov 19 '18 at 15:11












1 Answer
1






active

oldest

votes


















1














This is how you can do it,



First define custom style for CalendarDataButton based on standard style



    <Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
<Style.Triggers>
<Trigger Property="IsBlackedOut" Value="True">
<Setter Property="Background" Value="DeepPink"/>
<Setter Property="local:CalenderHelper.IsBlackOutDisabled" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</Calendar.CalendarDayButtonStyle>
<Calendar.BlackoutDates>
<CalendarDateRange Start="21-Nov-2018" End="21-Nov-2018"/>
<CalendarDateRange Start="25-Nov-2018" End="25-Nov-2018"/>
</Calendar.BlackoutDates>
</Calendar>


In order to remove the strike mark from Blackout date, you will have to use the below attached property 'IsBlackOutDisabled'



public class CalenderHelper : DependencyObject
{
public static readonly DependencyProperty IsBlackOutDisabledProperty =
DependencyProperty.RegisterAttached("IsBlackOutDisabled", typeof(bool), typeof(CalenderHelper), new PropertyMetadata(false, OnIsBlackOutDisabledChanged));

public static bool GetIsBlackOutDisabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsBlackOutDisabledProperty);
}

public static void SetIsBlackOutDisabled(DependencyObject obj, bool value)
{
obj.SetValue(IsBlackOutDisabledProperty, value);
}

private static void OnIsBlackOutDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CalendarDayButton dayButton = d as 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) as Path;
if (collapsed)
blackoutPath.Visibility = System.Windows.Visibility.Collapsed;
else
blackoutPath.Visibility = System.Windows.Visibility.Visible;
}
}


UPDATE:



If you want the colored dates to be selectable by Calendar, you must go with different approach. Blackout Dates will not work for you.



This approach takes the converter to color the dates.



First define custom style for CalendarDataButton based on standard style



<Window.Resources>
<local:CustomLetterDayConverter x:Key="CustomLetterDayConverter" />
<Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource CustomLetterDayConverter}}" Value="{x:Null}">
<Setter Property="Background" Value="HotPink"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange" SelectedDate="{Binding SelectedDate}"
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle}"
>
</Calendar>
</Grid>


The custom converter



public class CustomLetterDayConverter : IValueConverter
{
static HashSet<DateTime> dict = new HashSet<DateTime>();
static CustomLetterDayConverter()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(1));
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(5));
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";

return text;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}





share|improve this answer


























  • Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

    – George T Kurian
    Nov 20 '18 at 6:27











  • See my updates above to handle you scenario. Hope this helps.

    – RajN
    Nov 20 '18 at 12:03











  • Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

    – George T Kurian
    Nov 21 '18 at 8:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371091%2fdisplaying-dates-as-holiday-in-wpf-calendar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is how you can do it,



First define custom style for CalendarDataButton based on standard style



    <Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
<Style.Triggers>
<Trigger Property="IsBlackedOut" Value="True">
<Setter Property="Background" Value="DeepPink"/>
<Setter Property="local:CalenderHelper.IsBlackOutDisabled" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</Calendar.CalendarDayButtonStyle>
<Calendar.BlackoutDates>
<CalendarDateRange Start="21-Nov-2018" End="21-Nov-2018"/>
<CalendarDateRange Start="25-Nov-2018" End="25-Nov-2018"/>
</Calendar.BlackoutDates>
</Calendar>


In order to remove the strike mark from Blackout date, you will have to use the below attached property 'IsBlackOutDisabled'



public class CalenderHelper : DependencyObject
{
public static readonly DependencyProperty IsBlackOutDisabledProperty =
DependencyProperty.RegisterAttached("IsBlackOutDisabled", typeof(bool), typeof(CalenderHelper), new PropertyMetadata(false, OnIsBlackOutDisabledChanged));

public static bool GetIsBlackOutDisabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsBlackOutDisabledProperty);
}

public static void SetIsBlackOutDisabled(DependencyObject obj, bool value)
{
obj.SetValue(IsBlackOutDisabledProperty, value);
}

private static void OnIsBlackOutDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CalendarDayButton dayButton = d as 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) as Path;
if (collapsed)
blackoutPath.Visibility = System.Windows.Visibility.Collapsed;
else
blackoutPath.Visibility = System.Windows.Visibility.Visible;
}
}


UPDATE:



If you want the colored dates to be selectable by Calendar, you must go with different approach. Blackout Dates will not work for you.



This approach takes the converter to color the dates.



First define custom style for CalendarDataButton based on standard style



<Window.Resources>
<local:CustomLetterDayConverter x:Key="CustomLetterDayConverter" />
<Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource CustomLetterDayConverter}}" Value="{x:Null}">
<Setter Property="Background" Value="HotPink"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange" SelectedDate="{Binding SelectedDate}"
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle}"
>
</Calendar>
</Grid>


The custom converter



public class CustomLetterDayConverter : IValueConverter
{
static HashSet<DateTime> dict = new HashSet<DateTime>();
static CustomLetterDayConverter()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(1));
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(5));
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";

return text;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}





share|improve this answer


























  • Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

    – George T Kurian
    Nov 20 '18 at 6:27











  • See my updates above to handle you scenario. Hope this helps.

    – RajN
    Nov 20 '18 at 12:03











  • Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

    – George T Kurian
    Nov 21 '18 at 8:42
















1














This is how you can do it,



First define custom style for CalendarDataButton based on standard style



    <Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
<Style.Triggers>
<Trigger Property="IsBlackedOut" Value="True">
<Setter Property="Background" Value="DeepPink"/>
<Setter Property="local:CalenderHelper.IsBlackOutDisabled" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</Calendar.CalendarDayButtonStyle>
<Calendar.BlackoutDates>
<CalendarDateRange Start="21-Nov-2018" End="21-Nov-2018"/>
<CalendarDateRange Start="25-Nov-2018" End="25-Nov-2018"/>
</Calendar.BlackoutDates>
</Calendar>


In order to remove the strike mark from Blackout date, you will have to use the below attached property 'IsBlackOutDisabled'



public class CalenderHelper : DependencyObject
{
public static readonly DependencyProperty IsBlackOutDisabledProperty =
DependencyProperty.RegisterAttached("IsBlackOutDisabled", typeof(bool), typeof(CalenderHelper), new PropertyMetadata(false, OnIsBlackOutDisabledChanged));

public static bool GetIsBlackOutDisabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsBlackOutDisabledProperty);
}

public static void SetIsBlackOutDisabled(DependencyObject obj, bool value)
{
obj.SetValue(IsBlackOutDisabledProperty, value);
}

private static void OnIsBlackOutDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CalendarDayButton dayButton = d as 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) as Path;
if (collapsed)
blackoutPath.Visibility = System.Windows.Visibility.Collapsed;
else
blackoutPath.Visibility = System.Windows.Visibility.Visible;
}
}


UPDATE:



If you want the colored dates to be selectable by Calendar, you must go with different approach. Blackout Dates will not work for you.



This approach takes the converter to color the dates.



First define custom style for CalendarDataButton based on standard style



<Window.Resources>
<local:CustomLetterDayConverter x:Key="CustomLetterDayConverter" />
<Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource CustomLetterDayConverter}}" Value="{x:Null}">
<Setter Property="Background" Value="HotPink"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange" SelectedDate="{Binding SelectedDate}"
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle}"
>
</Calendar>
</Grid>


The custom converter



public class CustomLetterDayConverter : IValueConverter
{
static HashSet<DateTime> dict = new HashSet<DateTime>();
static CustomLetterDayConverter()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(1));
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(5));
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";

return text;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}





share|improve this answer


























  • Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

    – George T Kurian
    Nov 20 '18 at 6:27











  • See my updates above to handle you scenario. Hope this helps.

    – RajN
    Nov 20 '18 at 12:03











  • Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

    – George T Kurian
    Nov 21 '18 at 8:42














1












1








1







This is how you can do it,



First define custom style for CalendarDataButton based on standard style



    <Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
<Style.Triggers>
<Trigger Property="IsBlackedOut" Value="True">
<Setter Property="Background" Value="DeepPink"/>
<Setter Property="local:CalenderHelper.IsBlackOutDisabled" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</Calendar.CalendarDayButtonStyle>
<Calendar.BlackoutDates>
<CalendarDateRange Start="21-Nov-2018" End="21-Nov-2018"/>
<CalendarDateRange Start="25-Nov-2018" End="25-Nov-2018"/>
</Calendar.BlackoutDates>
</Calendar>


In order to remove the strike mark from Blackout date, you will have to use the below attached property 'IsBlackOutDisabled'



public class CalenderHelper : DependencyObject
{
public static readonly DependencyProperty IsBlackOutDisabledProperty =
DependencyProperty.RegisterAttached("IsBlackOutDisabled", typeof(bool), typeof(CalenderHelper), new PropertyMetadata(false, OnIsBlackOutDisabledChanged));

public static bool GetIsBlackOutDisabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsBlackOutDisabledProperty);
}

public static void SetIsBlackOutDisabled(DependencyObject obj, bool value)
{
obj.SetValue(IsBlackOutDisabledProperty, value);
}

private static void OnIsBlackOutDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CalendarDayButton dayButton = d as 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) as Path;
if (collapsed)
blackoutPath.Visibility = System.Windows.Visibility.Collapsed;
else
blackoutPath.Visibility = System.Windows.Visibility.Visible;
}
}


UPDATE:



If you want the colored dates to be selectable by Calendar, you must go with different approach. Blackout Dates will not work for you.



This approach takes the converter to color the dates.



First define custom style for CalendarDataButton based on standard style



<Window.Resources>
<local:CustomLetterDayConverter x:Key="CustomLetterDayConverter" />
<Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource CustomLetterDayConverter}}" Value="{x:Null}">
<Setter Property="Background" Value="HotPink"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange" SelectedDate="{Binding SelectedDate}"
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle}"
>
</Calendar>
</Grid>


The custom converter



public class CustomLetterDayConverter : IValueConverter
{
static HashSet<DateTime> dict = new HashSet<DateTime>();
static CustomLetterDayConverter()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(1));
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(5));
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";

return text;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}





share|improve this answer















This is how you can do it,



First define custom style for CalendarDataButton based on standard style



    <Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}">
<Style.Triggers>
<Trigger Property="IsBlackedOut" Value="True">
<Setter Property="Background" Value="DeepPink"/>
<Setter Property="local:CalenderHelper.IsBlackOutDisabled" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</Calendar.CalendarDayButtonStyle>
<Calendar.BlackoutDates>
<CalendarDateRange Start="21-Nov-2018" End="21-Nov-2018"/>
<CalendarDateRange Start="25-Nov-2018" End="25-Nov-2018"/>
</Calendar.BlackoutDates>
</Calendar>


In order to remove the strike mark from Blackout date, you will have to use the below attached property 'IsBlackOutDisabled'



public class CalenderHelper : DependencyObject
{
public static readonly DependencyProperty IsBlackOutDisabledProperty =
DependencyProperty.RegisterAttached("IsBlackOutDisabled", typeof(bool), typeof(CalenderHelper), new PropertyMetadata(false, OnIsBlackOutDisabledChanged));

public static bool GetIsBlackOutDisabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsBlackOutDisabledProperty);
}

public static void SetIsBlackOutDisabled(DependencyObject obj, bool value)
{
obj.SetValue(IsBlackOutDisabledProperty, value);
}

private static void OnIsBlackOutDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CalendarDayButton dayButton = d as 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) as Path;
if (collapsed)
blackoutPath.Visibility = System.Windows.Visibility.Collapsed;
else
blackoutPath.Visibility = System.Windows.Visibility.Visible;
}
}


UPDATE:



If you want the colored dates to be selectable by Calendar, you must go with different approach. Blackout Dates will not work for you.



This approach takes the converter to color the dates.



First define custom style for CalendarDataButton based on standard style



<Window.Resources>
<local:CustomLetterDayConverter x:Key="CustomLetterDayConverter" />
<Style x:Key="CalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource CustomLetterDayConverter}}" Value="{x:Null}">
<Setter Property="Background" Value="HotPink"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Calendar x:Name="HolidayCalendar" SelectionMode="MultipleRange" SelectedDate="{Binding SelectedDate}"
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle}"
>
</Calendar>
</Grid>


The custom converter



public class CustomLetterDayConverter : IValueConverter
{
static HashSet<DateTime> dict = new HashSet<DateTime>();
static CustomLetterDayConverter()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(1));
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(5));
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";

return text;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 12:02

























answered Nov 19 '18 at 18:24









RajNRajN

3,64521021




3,64521021













  • Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

    – George T Kurian
    Nov 20 '18 at 6:27











  • See my updates above to handle you scenario. Hope this helps.

    – RajN
    Nov 20 '18 at 12:03











  • Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

    – George T Kurian
    Nov 21 '18 at 8:42



















  • Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

    – George T Kurian
    Nov 20 '18 at 6:27











  • See my updates above to handle you scenario. Hope this helps.

    – RajN
    Nov 20 '18 at 12:03











  • Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

    – George T Kurian
    Nov 21 '18 at 8:42

















Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

– George T Kurian
Nov 20 '18 at 6:27





Is it possible to select blackoutdates or can non selectable property of blackoutdate overridden?

– George T Kurian
Nov 20 '18 at 6:27













See my updates above to handle you scenario. Hope this helps.

– RajN
Nov 20 '18 at 12:03





See my updates above to handle you scenario. Hope this helps.

– RajN
Nov 20 '18 at 12:03













Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

– George T Kurian
Nov 21 '18 at 8:42





Thanks for the update. But I think this is not the solution. My GetHolidayBaseList is changing dynamically and I need to render the dynamic changes on the calendar. The current solution will set some random days in HotPink color, but what I want is apply color dynamically. For example: If the user changes the month to october (currently displayed month is november), then GetHolidayBaseList will have the holidays in October, but I need to update that on the Calendar display.

– George T Kurian
Nov 21 '18 at 8:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371091%2fdisplaying-dates-as-holiday-in-wpf-calendar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()