Displaying dates as holiday in WPF Calendar











up vote
0
down vote

favorite












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
    12 hours ago










  • 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
    12 hours ago















up vote
0
down vote

favorite












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
    12 hours ago










  • 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
    12 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 12 hours ago

























asked 19 hours ago









George T Kurian

187




187












  • Hey, I've edited my question. Will you please refer it again? Is my question clear now?
    – George T Kurian
    12 hours ago










  • 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
    12 hours ago


















  • Hey, I've edited my question. Will you please refer it again? Is my question clear now?
    – George T Kurian
    12 hours ago










  • 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
    12 hours ago
















Hey, I've edited my question. Will you please refer it again? Is my question clear now?
– George T Kurian
12 hours ago




Hey, I've edited my question. Will you please refer it again? Is my question clear now?
– George T Kurian
12 hours ago












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
12 hours ago




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
12 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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;
}
}





share|improve this answer





















    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',
    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








    up vote
    0
    down vote













    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;
    }
    }





    share|improve this answer

























      up vote
      0
      down vote













      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;
      }
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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;
        }
        }





        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;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 9 hours ago









        RajN

        3,3432921




        3,3432921






























             

            draft saved


            draft discarded



















































             


            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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen