如何从 C# 中的 DatePicker(WPF) 获取价值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41447490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get value from DatePicker(WPF) in C#?
提问by droft1312
I have a DatePickerclass 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 DateTimePickerin WinForms but it didn't work.
我的程序中有一个DatePicker类,但是如何从中获取实际值,例如 19.12.2017?我尝试应用在 WinForms 中用于从DateTimePicker获取值的方式,但没有奏效。
回答by mm8
You get the selected value from the SelectedDate property of the DatePicker.
您可以从 DatePicker 的 SelectedDate 属性获得选定的值。
<DatePicker x:Name="dp" />
DateTime? selectedDate = dp.SelectedDate;
if(selectedDate.HasValue)
{
string formatted = selectedDate.Value.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}

