我在我的WPF MVVM應用程序中具有以下樣式。添加「今日按鈕」日曆控件
<Style x:Key="DefaultCalendar" TargetType="Calendar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Calendar}">
<StackPanel HorizontalAlignment="Center" Name="PART_Root">
<CalendarItem Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Name="PART_CalendarItem" Style="{TemplateBinding Calendar.CalendarItemStyle}" />
<Button Content="Today" Command="Commands:CalendarCommands.SelectToday" CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我創建的命令類
namespace Viterra.Freight.Client.Resources.Commands
{
public class CalendarCommands
{
public static RoutedCommand SelectToday = new RoutedCommand("Today", typeof(CalendarCommands));
}
}
,並顯示日曆打開時,然而,該按鈕被禁用。我確信我需要使用這個路由命令做更多的事情,所以它設置日期,但我不知道什麼或在哪裏。
編輯:最終解決 請注意,我還修改的DatePicker使用該日曆的風格,所有的時間。
<Style x:Key="DefaultCalendar" TargetType="Calendar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Calendar}">
<StackPanel HorizontalAlignment="Center" Name="PART_Root">
<CalendarItem Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Name="PART_CalendarItem" Style="{TemplateBinding Calendar.CalendarItemStyle}" />
<Button Content="Today" Command="Commands:CalendarCommands.SelectToday" CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- style for default datepicker -->
<Style x:Key="DefaultDatePicker" TargetType="DatePicker">
<Setter Property="Height" Value="23"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="CalendarStyle" Value="{StaticResource DefaultCalendar}" />
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource DateTimeValidationTemplate}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
而且CalendarCommands.cs
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;
}
}
}
}
或者無論最好的辦法是向DatePicker添加「今日」按鈕 – CaffGeek