WPF DatePicker 默认为今天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3885912/
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
WPF DatePicker default to today's date
提问by developer
WPF DatePicker always show 'Show Calendar' by default. I want it to show current/todays date. How do I do that. I tried doing something like the below in the constructor but it won't work,
WPF DatePicker 默认总是显示“显示日历”。我希望它显示当前/今天的日期。我怎么做。我尝试在构造函数中做类似下面的事情,但它不起作用,
datePicker.SelectedDate = DateTime.Now.Date;
or
或者
datePicker.DisplayDate = DateTime.Now.Date;
回答by Kishore Kumar
please try with this ....
请试试这个....
<my:DatePicker SelectedDate="{x:Static sys:DateTime.Now}"/>
add this reference
添加此参考
xmlns:sys="clr-namespace:System;assembly=mscorlib"
回答by leoinlios
The below works for me. (dpDate is my DatePicker control)
以下对我有用。(dpDate 是我的 DatePicker 控件)
public MainWindow()
{
InitializeComponent();
dpDate.SelectedDate = DateTime.Today;
}
回答by Aaron McIver
Couple of options...
几个选项...
You could copy the entire style for the DatePicker control and edit the XAML and use that as the default resource making the TargetType DatePicker which will force it to be used across the application.
您可以复制 DatePicker 控件的整个样式并编辑 XAML 并将其用作创建 TargetType DatePicker 的默认资源,这将强制它在整个应用程序中使用。
You could also edit the style locally and place your own TextBox in the style and hide the DatePickerTextBox and then setup the binding appropriately.
您还可以在本地编辑样式并将您自己的 TextBox 放在样式中并隐藏 DatePickerTextBox,然后适当地设置绑定。
Some in depth conversation herewith a pretty good explanation by ericf at the top.
这里有一些深入的对话,顶部的 ericf 有一个很好的解释。
回答by Shahar Zoleha
the issue is you are attempting to access date with time. that doesn't work.
问题是您正在尝试随时间访问日期。那行不通。
the date today is represented in "DateTime.Today"
今天的日期用“DateTime.Today”表示
回答by Kenneth
You can use the DatePicker's Text property to get or set the date http://msdn.microsoft.com/en-us/library/system.windows.controls.datepicker.text.aspx
您可以使用 DatePicker 的 Text 属性来获取或设置日期 http://msdn.microsoft.com/en-us/library/system.windows.controls.datepicker.text.aspx
I'm developing with IronPython and do it as so
我正在使用 IronPython 进行开发,并照此进行
self.root.FindName('DatePicker').Text = time.strftime('%m/%d/%Y')
回答by P_Fitz
If you need to set the date programmatically (or in the code behind) you could try...
如果您需要以编程方式(或在后面的代码中)设置日期,您可以尝试...
datePicker.Text = DateTime.Now.Date.ToString();