title: Adding "Today Button" to Calendar Control
body: <p>I have the following style in my WPF MVVM app.</p>
  
  <pre><code>&lt;Style x:Key="DefaultCalendar" TargetType="Calendar"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Calendar}"&gt;
                  &lt;StackPanel HorizontalAlignment="Center" Name="PART_Root"&gt;
                      &lt;CalendarItem Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Name="PART_CalendarItem" Style="{TemplateBinding Calendar.CalendarItemStyle}" /&gt;
                      &lt;Button Content="Today" Command="Commands:CalendarCommands.SelectToday" CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>I created the Commands class</p>
  
  <pre><code>namespace Viterra.Freight.Client.Resources.Commands
  {
      public class CalendarCommands
      {
          public static RoutedCommand SelectToday = new RoutedCommand("Today", typeof(CalendarCommands));
      }
  }
  </code></pre>
  
  <p>And it displays when the calendar opens, however, the button is disabled.  I'm sure I need to do more with this routed command so it sets the date, but I'm not sure what or where.</p>
  
  <p><strong>EDIT: Final Solution</strong>
  Note that I was also modifying the DatePicker to use this Calendar style, ALL the time.</p>
  
  <pre><code>&lt;Style x:Key="DefaultCalendar" TargetType="Calendar"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Calendar}"&gt;
                  &lt;StackPanel HorizontalAlignment="Center" Name="PART_Root"&gt;
                      &lt;CalendarItem Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Name="PART_CalendarItem" Style="{TemplateBinding Calendar.CalendarItemStyle}" /&gt;
                      &lt;Button Content="Today" Command="Commands:CalendarCommands.SelectToday" CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;!-- style for default datepicker --&gt;
  &lt;Style x:Key="DefaultDatePicker" TargetType="DatePicker"&gt;
      &lt;Setter Property="Height" Value="23"/&gt;
      &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
      &lt;Setter Property="CalendarStyle" Value="{StaticResource DefaultCalendar}" /&gt;
      &lt;Setter Property="Validation.ErrorTemplate" Value="{StaticResource DateTimeValidationTemplate}" /&gt;
      &lt;Style.Triggers&gt;
          &lt;Trigger Property="Validation.HasError" Value="true"&gt;
              &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>And the CalendarCommands.cs</p>
  
  <pre><code>using System;
  using System.Windows.Input;
  using System.Windows.Controls;
  
  namespace Viterra.Freight.Client.Resources.Commands
  {
      public static class CalendarCommands
      {
          private static readonly SelectTodayCommand _selectTodayCommand = new SelectTodayCommand();
  
          public static ICommand SelectToday
          {
              get { return _selectTodayCommand; }
          }
  
          private sealed class SelectTodayCommand : ICommand
          {
              public event EventHandler CanExecuteChanged
              {
                  add { CommandManager.RequerySuggested += value; }
                  remove { CommandManager.RequerySuggested -= value; }
              }
  
              public bool CanExecute(object parameter)
              {
                  return parameter is Calendar;
              }
  
              public void Execute(object parameter)
              {
                  var calendar = parameter as Calendar;
                  if (calendar == null) 
                      return;
  
                  var today = DateTime.Today;
                  calendar.SelectedDate = today;
                  calendar.DisplayDate = today;
              }
          }
      }
  }
  </code></pre>