问题描述
我有一个WPF Datagrid,其中一列是一个日期列。
I have a WPF Datagrid in which one of the columns is a Date Column.
所以我已经使用了一个DataTemplateColumn作为跟随
So i have used a DataTemplateColumn as Follows
<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>
问题是当我点击DatePicker控件时,默认日期设置为1/1/0001 ?
The problem is when i click on the DatePicker control the default date is set to 1/1/0001?
我如何设置我的datepicker设置为当前日期。
How can i set my datepicker to set to the current Date.
推荐答案
除非您在 DataContext
中有一个名为现在
的属性,否则 Bindings
将失败。相反,您应该使用 {x:Static}
语法:
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:
<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>
由于 DateTime
不在标准XAML命名空间,您需要向根元素添加一个xmlns声明:
Since DateTime
isn't in the standard XAML namespace, you need to add a xmlns declaration to the root element:
<UserControl xmlns:sys="clr-namespace:System;assembly=mscorlib" ...
这篇关于将WPF日期选择器的默认日期设置为当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!