将 WPF 日期选择器的默认日期设置为当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3662506/
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
Set the Default Date of WPF Date Picker to Current Date
提问by New Devleoper
I have a WPF Datagrid in which one of the columns is a Date Column.
我有一个 WPF Datagrid,其中一列是日期列。
So i have used a DataTemplateColumn as Follows
所以我使用了 DataTemplateColumn 如下
<my:DataGridTemplateColumn
CellTemplate="{StaticResource EffDateDateTimePickerControl}"
CellEditingTemplate="{StaticResource addrEffDate}"
Header="Effective Date"/>
And in my Resource File i have written the following code:
在我的资源文件中,我编写了以下代码:
<Style TargetType="{x:Type my:Calendar}" x:Key="CalenderControlTemplate">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="my:Calendar" >
<my:CalendarItem Name="myCalendarItem"
Background="White"
BorderBrush="Black"
BorderThickness="1"
VerticalAlignment="Center" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="EffDateDateTimePickerControl">
<Label x:Name="lblEffDate" Content="{Binding effectiveDate,Mode=TwoWay}" ></Label>
</DataTemplate>
<DataTemplate x:Key="addrEffDate">
<my:DatePicker x:Name="dpEffDate" Text="{Binding Path=effectiveDate,Mode=TwoWay}"
SelectedDate="{Binding Now}" DisplayDateStart="{Binding Now}"
CalendarStyle="{DynamicResource CalenderControlTemplate}" />
</DataTemplate>
The problem is when i click on the DatePicker control the default date is set to 1/1/0001?
问题是当我单击 DatePicker 控件时,默认日期设置为 1/1/0001?
How can i set my datepicker to set to the current Date.
如何将我的日期选择器设置为当前日期。
回答by Abe Heidebrecht
Unless you have a property in your DataContext
called Now
, your Bindings
will fail. Instead, you should be using the {x:Static}
syntax like so:
除非您在DataContext
被调用的属性中有属性,否则Now
您Bindings
将失败。相反,您应该使用如下{x:Static}
语法:
<DataTemplate x:Key="addrEffDate">
<my:DatePicker x:Name="dpEffDate" Text="{Binding Path=effectiveDate,Mode=TwoWay}"
SelectedDate="{x:Static sys:DateTime.Now}" DisplayDateStart="{x:Static sys:DateTime.Now}"
CalendarStyle="{DynamicResource CalenderControlTemplate}" />
</DataTemplate>
Since DateTime
isn't in the standard XAML namespace, you need to add a xmlns declaration to the root element:
由于DateTime
不在标准 XAML 命名空间中,您需要向根元素添加 xmlns 声明:
<UserControl xmlns:sys="clr-namespace:System;assembly=mscorlib" ...
回答by ChrisF
I think you need to replace
我认为你需要更换
DisplayDateStart
with
和
DisplayDate
Because DisplayDateStart
: (from the MSDN)
因为DisplayDateStart
:(来自MSDN)
Gets or sets the first date to be displayed.
获取或设置要显示的第一个日期。
not the date to display.
不是要显示的日期。
回答by SArifin
On top of Abe Heidebrecht's Answer I am providing an example. I think Abe's answer is correct. I had the same issue with new object and class binding and solved the problem in the way mentioned below:
在Abe Heidebrecht的回答之上,我提供了一个例子。我认为安倍的回答是正确的。我对新对象和类绑定有同样的问题,并以下面提到的方式解决了这个问题:
get
{
return (ClassDate - DateTime.MinValue).TotalDays == 0 ? DateTime.Now :ClassDate;
}
cheers :)
欢呼:)