tags:

views:

175

answers:

3

Hi,

I have a WPF Datagrid in which one of the columns is a Date Column.

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>

The problem is when i click on the DatePicker control the default date is set to 1/1/0001?

How can i set my datepicker to set to the current Date.

A: 

I think you need to replace

DisplayDateStart

with

DisplayDate

Because DisplayDateStart: (from the MSDN)

Gets or sets the first date to be displayed.

not the date to display.

ChrisF
+1  A: 

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>

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" ...
Abe Heidebrecht
A: 

@ ChrisF

I changed my code to what you have mentioned as :

<DataTemplate x:Key="addrEffDate">
        <my:DatePicker x:Name="dpEffDate" Text="{Binding     
           Path=effectiveDate,Mode=TwoWay}" Background="Transparent" 
           Foreground="Black"
           SelectedDate="{x:Static sys:DateTime.Now}"  
           DisplayDateStart="{x:Static sys:DateTime.Now}"
           CalendarStyle="{DynamicResource CalenderControlTemplate}" />
</DataTemplate>

DisplayDate and DisplayDateStart do not make a difference in the above code. I tested both. The Problem Now is that i can select a date but the selected date defaults to the current date and also it won't display the selected date. How do i get my selected date?

New Developer