Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get value from DatePicker(WPF) in C#?

I have a DatePicker class in my program, but how do I get an actual value, such as 19.12.2017, from it? I've tried to apply the way that is used to get the value from DateTimePicker in WinForms but it didn't work.

like image 682
droft1312 Avatar asked Jan 03 '17 16:01

droft1312


People also ask

How to use DatePicker in WPF?

Drag a DatePicker from the toolbox. The following example shows how to create a DatePicker control. When you click on any date from the DatePicker control, the program will update the title with that date. The following XAML code creates a DatePicker with some properties and click event.

Which control is used as a DatePicker?

The DatePicker control allows the user to select a date by either typing it into a text field or by using a drop-down Calendar control. Many of a DatePicker control's properties are for managing its built-in Calendar, and function identically to the equivalent property in Calendar.

How do I add a Calendar to WPF?

Set the DisplayMode property to Month, Year, or Decade. Specify whether the user can select a date, a range of dates, or multiple ranges of dates. Use the SelectionMode. Specify the range of dates that the Calendar displays.


1 Answers

You get the selected value from the SelectedDate property of the DatePicker.

<DatePicker x:Name="dp" />

DateTime? selectedDate = dp.SelectedDate;
if(selectedDate.HasValue)
{
    string formatted = selectedDate.Value.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
like image 163
mm8 Avatar answered Sep 25 '22 21:09

mm8