WPF 2015 Visual Studio DatePicker using C#
本文关键字:using DatePicker Studio 2015 Visual WPF | 更新日期: 2023-09-27 18:15:20
我有一个关于工具箱中的DatePicker的问题。我试图在WPF中为用户创建一个程序,从两个日期选择器中选择两个日期,并根据这些日期确定它们之间的天数。
我在网上找到了另一种叫做DateTimePicker的工具,它似乎更容易使用。但是,这并不包括在我的工具箱中,所以我不得不使用datepicker类。到目前为止,我的cs代码如下所示:using System;
using System.Collections.Generic;
namespace DateDifference
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static object RadDatePickerStart { get; private set; }
private void button_Click(object sender, RoutedEventArgs e)
{
}
private void startDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
// ... Get DatePicker reference.
var picker = sender as DatePicker;
// ... Get nullable DateTime from SelectedDate.
DateTime? date = picker.SelectedDate;
if (date == null)
{
// ... A null object.
this.Title = "No date";
}
else
{
// ... No need to display the time.
this.Title = "Program for calculating difference between dates";
textBoxResult.Text = date.Value.ToShortDateString();
}
}
private void endDate_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
{
// ... Get DatePicker reference.
var picker = sender as DatePicker;
// ... Get nullable DateTime from SelectedDate.
DateTime? date = picker.SelectedDate;
if (date == null)
{
// ... A null object.
this.Title = "No date";
}
else
{
// ... No need to display the time.
this.Title = "Program for calculating difference between dates";
textBoxResult.Text = date.Value.ToShortDateString();
}
}
}
}
所以我的问题是如何得到这个datepicker类的天的差异?
致以最亲切的问候!
您可以从日期选择器中获取datetime对象,然后像这样比较日期:
(EndDate - StartDate).TotalDays