在所有DatePickers上设置默认的DisplayDateStart

时间:2018-11-15 13:57:23

标签: c# wpf datepicker

我要为我的应用中的每个DatePicker申请一个固定的开始日期,因为重复以下代码段不是一个好主意。

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

除了将默认DisplayDateStart设置在一个位置外,我还需要能够绑定DatePicker的SelectedDate属性。

我尝试创建自定义UserControl,但是按日历图标却没有任何作用(嗯,文本框正在闪烁,但没有出现日历弹出窗口)

<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();
    }
  }
}

2 个答案:

答案 0 :(得分:3)

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

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

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

然后以全局默认样式将其用于DatePicker s:

<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>

我希望这个提示可以对您有所帮助

答案 1 :(得分:1)

PropertyMetadata对象采用默认值作为第一个参数。在此处指定您的日期...

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(new DateTime(2006,5,1)));

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