gpt4 book ai didi

.net - 添加 "Today Button"到日历控件

转载 作者:行者123 更新时间:2023-12-02 08:44:38 24 4
gpt4 key购买 nike

我的 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;
}
}
}
}

最佳答案

或者,如果您不想使用代码隐藏和 CommandBindings,您可以使用简单的 ICommand 实现:

public static class CalendarCommands
{
private static readonly SelectTodayCommand _selectToday = new SelectTodayCommand();

public static ICommand SelectToday
{
get { return _selectToday; }
}

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)
{
DateTime today = DateTime.Today;
calendar.SelectedDate = today;
calendar.DisplayDate = today;
}
}
}
}

关于.net - 添加 "Today Button"到日历控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13382726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com