我的 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>
我创建了 Commands 类
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;
}
}
}
}