Wpf自定义datepicker用户控件

我想创建一个用户控件来获取用户的日期。 它应该有三个文本框,一个用于年,月和日。 我不知道如何创建它。

           

代码背后

 public partial class ChooseDateControl : UserControl { public static readonly DependencyProperty ValueProperty; public static readonly DependencyProperty YearProperty; public static readonly DependencyProperty MonthProperty; public static readonly DependencyProperty DayProperty; static ChooseDateControl() { ValueProperty = DependencyProperty.Register( "Value", typeof(DateTime), typeof(ChooseDateControl), new FrameworkPropertyMetadata(DateTime.MinValue)); ValueProperty = DependencyProperty.Register( "Year", typeof(int), typeof(ChooseDateControl), new FrameworkPropertyMetadata((int)0)); ValueProperty = DependencyProperty.Register( "Month", typeof(int), typeof(ChooseDateControl), new FrameworkPropertyMetadata((int)0)); ValueProperty = DependencyProperty.Register( "Day", typeof(int), typeof(ChooseDateControl), new FrameworkPropertyMetadata((int)0)); } public ChooseDateControl() { InitializeComponent(); } public DateTime Value { get { return (DateTime)base.GetValue(ValueProperty); } set { base.SetValue(ValueProperty, value); } } public int Year { get { return (int)base.GetValue(YearProperty); } set { base.SetValue(YearProperty, value); } } public int Month { get { return (int)base.GetValue(MonthProperty); } set { base.SetValue(MonthProperty, value); } } public int Day { get { return (int)base.GetValue(DayProperty); } set { base.SetValue(DayProperty, value); } } } 

它无法正常工作 – 它返回默认值,即DateTime.MinValue。 请帮我。

编写像这样的逻辑类型可能更好:

 static void Main(string[] args) { Console.WriteLine("Year"); var year = Int32.Parse(Console.ReadLine()); Console.WriteLine("Month"); var month = Int32.Parse(Console.ReadLine()); Console.WriteLine("Day"); var day = Int32.Parse(Console.ReadLine()); var customDate = new DateTime(year, month, day); Console.WriteLine(customDate); Console.ReadLine(); } 

您可以实现类似于具有按钮的按钮,按下该按钮将获取值并创建新的日期时间值。 有许多方法可以从输入值生成DateTime,只需浏览网页,您就可以在WPF中找到大量关于此的示例和教程。 希望这会给你一些想法并帮助你。

为什么不直接使用WPF Toolkit中的CalendarDatePicker控件? 你没有在你的问题中提到任何自定义的东西,所以这应该对你有效。

您没有将文本框连接到依赖项属性。 永远不会设置该值。 您需要某种类型的转换器,它会将文本框中的值转换为实际日期。 我认为你最好使用工具包(3.5)或框架(4.0)中的DatePicker控件。

对于初学者停止复制和粘贴:0)。 您的所有DP注册均适用于ValueProperty 。 并且您必须处理每个属性的属性更改回调以更新Value属性。