Created
November 7, 2016 01:59
-
-
Save ObserverHerb/9210a00fe66e2710bcc06650c2621d74 to your computer and use it in GitHub Desktop.
Highlighted WPF Calendar Control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="HighlightCalendar.HighlightCalendarWindow" | |
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:HighlightCalendar" | |
mc:Ignorable="d" | |
Title="HighlightCalendarWindow" Height="350" Width="525" Loaded="Window_Loaded"> | |
<Grid> | |
<Viewbox VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> | |
<Calendar x:Name="calendar"> | |
<Calendar.CalendarDayButtonStyle> | |
<Style TargetType="CalendarDayButton" BasedOn="{StaticResource {x:Type CalendarDayButton}}"> | |
<EventSetter Event="Loaded" Handler="calendarButton_Loaded" /> | |
</Style> | |
</Calendar.CalendarDayButtonStyle> | |
</Calendar> | |
</Viewbox> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Controls.Primitives; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
namespace HighlightCalendar | |
{ | |
public partial class HighlightCalendarWindow : Window | |
{ | |
private List<DateTime> significantDates; | |
public HighlightCalendarWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void Window_Loaded(object sender, RoutedEventArgs e) | |
{ | |
significantDates = new List<DateTime> | |
{ | |
DateTime.Parse("2016/11/15"), | |
DateTime.Parse("2016/11/26"), | |
DateTime.Parse("2016/11/30") | |
}; | |
} | |
private void calendarButton_Loaded(object sender, EventArgs e) | |
{ | |
CalendarDayButton button = (CalendarDayButton)sender; | |
DateTime date = (DateTime)button.DataContext; | |
HighlightDay(button, date); | |
button.DataContextChanged += new DependencyPropertyChangedEventHandler(calendarButton_DataContextChanged); | |
} | |
private void HighlightDay(CalendarDayButton button, DateTime date) | |
{ | |
if (significantDates.Contains(date)) | |
button.Background = Brushes.LightBlue; | |
else | |
button.Background = Brushes.White; | |
} | |
private void calendarButton_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) | |
{ | |
CalendarDayButton button = (CalendarDayButton)sender; | |
DateTime date = (DateTime)button.DataContext; | |
HighlightDay(button, date); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment