Calendar Control Tools In Windows Presentation Foundation

Introduction

This writing is about Calendar Control in Windows Presentation Foundation. Here I will be working with a demo of using Calendar in WPF on Visual Studio 2015.

Technical Requirements
  1. Visual Studio 2015 installed on your PC.
About Calendar Control

Calendar Control allows you to select a date using graphical calendar display. I would like to list out the methods, properties and events of Calendar Control.

List of methods on Calendar Control

S.No. List of Methods
01. OnApplyTemplate
02. ToString

List of Properties on Calendar Control

S.No. List of Properties
01. BlackoutDates
02. CalendarButtonStyle
03. CalendarDayButtonStyle
04. CalendarItemStyle
05. DisplayDate
06. DisplayDateStart
07. DisplayDateEnd
08. DisplayMode
09. FirstDayOfWeek
10. IsTodayHighlighted
11. SelectedDate
12. SelectedDates
13. SelectionMode

List of Events in Calendar Class

S.No. List of Events
01. DisplayDateChanged
02. DisplayModeChanged
03. SelectedDatesChanged
04. SelectionModeChanged

Demo for using Calendar Control in WPF

Run Visual Studio 2015 in your PC and select File - New - Project.



Select Visual C# - Windows - WPF Application - Name your WPF Application and select the location where you need it to be saved and click on OK.



This will create the project names “CalendarWPF”.

Drag and drop the Calendar Tool in the Graphical Window of your Visual Studio as shown below.



You can find the below code on MainWindow.xaml once when you drag and drop the Calendar tool in your Graphical Window.
  1. <Calendar HorizontalAlignment="Left" Margin="144,91,0,0" VerticalAlignment="Top"/>  
Run the WPF Application now by clicking on “Start” at the top pane of the Visual Studio 2015 to check the window with the calendar.



You can also rename the title for this window as were doing on the previous demo’s. Goto MainWindow.xaml and make the following changes on your code.

Existing Code on MainWindow.xaml
 
  1. <Window x:Class="CalendarWPF.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6. xmlns:local="clr-namespace:CalendarWPF"  
  7. mc:Ignorable="d"  
  8. Title="MainWindow" Height="350" Width="525">  
Code to be replaced on MainWindow.xaml  
  1. <Window x:Class="CalendarWPF.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility2006"  
  6. xmlns:local="clr-namespace:CalendarWPF"  
  7. mc:Ignorable="d"  
  8. Title="Calendar Window" Height="350" Width="525">  
You can find the below changes on your Graphical Window, you can even try the same by checking your output when you run the WPF Application that you have developed.






Tips

You can also try all properties with help of the below code on Visual Studio for Calendar Control,
  1. <Window x:Class="WPFCalenderControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFCalenderControl" mc:Ignorable="d" Title="Calendar Window" Height="350" Width="604">  
  2.     <Grid>  
  3.         <Calendar Margin="20" SelectionMode="MultipleRange" IsTodayHighlighted="false" DisplayDate="1/1/2015" DisplayDateEnd="1/31/2015" SelectedDatesChanged="Calendar_SelectedDatesChanged" xmlns:sys="clr-namespace:System;assembly = mscorlib">  
  4.             <Calendar.BlackoutDates>  
  5.                 <CalendarDateRange Start="1/2/2015" End="1/4/2015" />  
  6.                 <CalendarDateRange Start="1/9/2015" End="1/9/2015" />  
  7.                 <CalendarDateRange Start="1/16/2015" End="1/16/2015" />  
  8.                 <CalendarDateRange Start="1/23/2015" End="1/25/2015" />  
  9.                 <CalendarDateRange Start="1/30/2015" End="1/30/2015" /> </Calendar.BlackoutDates>  
  10.             <Calendar.SelectedDates>  
  11.                 <sys:DateTime>1/5/2015</sys:DateTime>  
  12.                 <sys:DateTime>1/12/2015</sys:DateTime>  
  13.                 <sys:DateTime>1/14/2015</sys:DateTime>  
  14.                 <sys:DateTime>1/13/2015</sys:DateTime>  
  15.                 <sys:DateTime>1/15/2015</sys:DateTime>  
  16.                 <sys:DateTime>1/27/2015</sys:DateTime>  
  17.                 <sys:DateTime>4/2/2015</sys:DateTime>  
  18.             </Calendar.SelectedDates>  
  19.             <Calendar.Background>  
  20.                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">  
  21.                     <GradientStop Color="#FFE4EAF0" Offset="0" />  
  22.                     <GradientStop Color="#FFECF0F4" Offset="0.16" />  
  23.                     <GradientStop Color="#FFFCFCFD" Offset="0.16" />  
  24.                     <GradientStop Color="#FFD80320" Offset="1" /> </LinearGradientBrush>  
  25.             </Calendar.Background>  
  26.         </Calendar>  
  27.     </Grid>  
  28. </Window>  
  Keynotes in Short
  • About Calendar Tools in WPF.
  • List of properties on Calendar Control.
  • List of methods on Calendar Control.
  • List of Events on Calendar Control.
  • Demo on using Calendar Control in WPF.
  • Sample code for methods, properties and events in WPF
Ebook Download
View all
Learn
View all