Displaying dates as holiday in WPF Calendar
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
I want to achieve something like this - Holidays marked as Red
as in this link
Any help to write a control template required for this functionality is appreciated.
Thanks in advance!
c# wpf wpf-controls
add a comment |
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
I want to achieve something like this - Holidays marked as Red
as in this link
Any help to write a control template required for this functionality is appreciated.
Thanks in advance!
c# wpf wpf-controls
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
add a comment |
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
I want to achieve something like this - Holidays marked as Red
as in this link
Any help to write a control template required for this functionality is appreciated.
Thanks in advance!
c# wpf wpf-controls
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
I want to achieve something like this - Holidays marked as Red
as in this link
Any help to write a control template required for this functionality is appreciated.
Thanks in advance!
c# wpf wpf-controls
c# wpf wpf-controls
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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();
}
}
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. MyGetHolidayBaseList
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), thenGetHolidayBaseList
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
}
}
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. MyGetHolidayBaseList
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), thenGetHolidayBaseList
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
add a comment |
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();
}
}
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. MyGetHolidayBaseList
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), thenGetHolidayBaseList
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
add a comment |
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();
}
}
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();
}
}
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. MyGetHolidayBaseList
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), thenGetHolidayBaseList
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
add a comment |
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. MyGetHolidayBaseList
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), thenGetHolidayBaseList
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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