c# - 在所有 DatePicker 上设置默认 DisplayDateStart

标签 c# wpf datepicker

我想为应用程序中的每个 DatePicker 应用一个设定的开始日期,因为重复以下代码片段不是一个好主意。

<DatePicker SelectedDate="{Binding Date}"
            DisplayDateStart="05/01/2006"/>

除了在一个地方设置默认的 DisplayDateStart 之外,我还需要能够绑定(bind) DatePicker 的 SelectedDate 属性。

我尝试创建自定义用户控件,但按日历图标没有任何反应(好吧,文本框正在闪烁,但没有出现日历弹出窗口)

<UserControl x:Class="MyWpfAdd.ModDatePicker"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel>
        DatePicker x:Name="DatePicker" DisplayDateStart="05/01/2006"/>
    </DockPanel>
</UserControl>

使用底层代码:

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace MyWpfApp
{
  public partial class ModDatePicker : UserControl
  {
    public DateTime? SelectedDate
    {
      get => (DateTime?)GetValue(SelectedDateProperty);
      set
      {
        this.DatePicker.SelectedDate = value;
        SetValue(SelectedDateProperty, value);
      }
    }

    public static readonly DependencyProperty SelectedDateProperty =
      DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(ModDatePicker), new PropertyMetadata(null));

    public DateTime? DisplayDateEnd
    {
      get => (DateTime?)GetValue(DisplayDateEndProperty);
      set
      {
        this.DatePicker.DisplayDateEnd = value;
        SetValue(DisplayDateEndProperty, value);
      }
    }

    public static readonly DependencyProperty DisplayDateEndProperty =
      DependencyProperty.Register("DisplayDateEnd", typeof(DateTime?), typeof(ModDatePicker), new PropertyMetadata(null));

    public ModDatePicker()
    {
      InitializeComponent();
    }
  }
}

最佳答案

首先创建一个具有静态属性的类,该类提供您需要的默认日期:

public static class DatePickerHelper
{
    private static DateTime defaultDisplayDateStart = new DateTime(2006, 1, 5);

    public static DateTime DefaultDisplayDateStart
    {
        get
        {
            return defaultDisplayDateStart;
        }
    }
}

然后在 DatePicker 的全局默认样式中使用它:

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"

             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource {x:Type DatePicker}}">
            <Setter Property="DisplayDateStart" Value="{x:Static local:DatePickerHelper.DefaultDisplayDateStart}" />
        </Style>
    </Application.Resources>
</Application>

希望这个提示能帮到你

关于c# - 在所有 DatePicker 上设置默认 DisplayDateStart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53321073/

相关文章:

c# - 获取按钮中的 TextBox 文本值

c# - html内容的总宽度,使用c#

c# - WPF:在派生自 WPF TextBox 的控件中,没有为空格键调用 OnKeyDown()

c# - WriteableBitmap 不能在单独的线程上工作?

javascript - 将 setDate 与 jQueryUi 日期选择器一起使用

c# - 找不到指定的过程。 (来自 HRESULT : 0x8007007F) 的异常

c# - 将选项设置为 FbTransaction

c# - WPF中如何使用数据注解进行单位换算

jquery - 如何正确实现 jQuery 日期选择器和 jQuery 时间选择器

jquery - 如何将日期渲染为 jQuery 日期选择器的链接?