none
Change the Default Date in the Date Picker Control

    Question

  • 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.

    Tuesday, September 07, 2010 8:19 PM

Answers

  • Hi Min Zhu,

    I tried the following code today and it works. I am not sure if it is a good way to do it or not. But Heyy it works.

    C# Code behind:

    Microsoft.Windows.Controls.

    DatePicker _dpEffDate;

     

     

    private void dpContactEffDateAtt1_Loaded(object sender, RoutedEventArgs e)

    {

    _dpEffDate = sender

    as Microsoft.Windows.Controls.DatePicker;

    _dpEffDate.DisplayDate =

    DateTime.Now;

    _dpEffDate.SelectedDate =

    DateTime.Now;

    }

    And my XAML to the following :

     

     

    <DataTemplate x:Key="ContactEffDateTimePickerControlAtt1">

     

     

    <Label x:Name="lblContactEffDateAtt1" Background="Transparent" Foreground="Black"

     

    Content="{Binding contact_EffDate,Mode=TwoWay}"></Label>

     

     

    </DataTemplate>

     

     

    <DataTemplate x:Key="contactEffDateAtt1">

     

     

    <my:DatePicker x:Name="dpContactEffDateAtt1"

     

    SelectedDate="{Binding Path = contact_EffDate,Mode=TwoWay}"

     

    DisplayDate="{Binding Path = contact_EffDate,Mode=TwoWay}"

     

    CalendarStyle="{DynamicResource CalenderControlTemplate}"

     

    Background="Transparent" Foreground="Black" Loaded="dpContactEffDateAtt1_Loaded" >

     

     

     

    </my:DatePicker>

     

     

    </DataTemplate>

    And it works.

    • Edited by NewtoLinq Tuesday, September 14, 2010 5:02 PM Change
    • Marked as answer by NewtoLinq Tuesday, September 14, 2010 5:02 PM
    Tuesday, September 14, 2010 4:39 PM

All replies

  • Hi

    Please follow the link, it ll be very fruitful for you

    http://msdn.microsoft.com/en-us/library/aa970773.aspx

    And also have look on it, if you got some time 

    http://msdn.microsoft.com/en-us/magazine/dd882520.aspx

    http://www.codeproject.com/KB/selection/vhCalendar.aspx

    Regards

    Zee

    Wednesday, September 08, 2010 1:33 AM
  • Hi,

    You need to set the SelectedDate property of the datepicker.


    Syed Mustehsan Ikram
    Wednesday, September 08, 2010 3:09 AM
  • @Syed Mustehsan Ikram

    I already set the Selected Date property. It does not help. Any other ideas?

    Wednesday, September 08, 2010 4:08 AM
  • Hi,

    I think the problem is ur Now property is not Binding to the selecteddate of the datepicker as it is a part of the Datatemplate

    try using relativesource

    <DataTemplate x:Key="addrEffDate">
    
    <my:DatePicker x:Name="dpEffDate" Text="{Binding Path=effectiveDate,Mode=TwoWay}" SelectedDate="{Binding Now,Relativesource RelativeSource={RelativeSource Mode=FindAncestor,
                  AncestorType={x:Type Put ParentControl Of this DataTemplate}}}" DisplayDateStart="{Binding Now,put relativesource here as well}"
    
     CalendarStyle="{DynamicResource CalenderControlTemplate}" />
    
     
    
     
    
    </DataTemplate>
    
    hope this will help u
    Wednesday, September 08, 2010 4:35 AM
  • Hi,

    I think the property which is providing data to SelectedDate property of DatePicker has not been set to e.g DateTime.Now.

    and you dont need to bind the DisplayDate property of DatePicker because when you set the SelectedDate property then DisplayDate will automatically be set by datepicker.


    Syed Mustehsan Ikram
    Wednesday, September 08, 2010 9:44 AM
  • @RaoBhavik

    I changed my Code to the following. And it still does not work !!!

     

     

    <my:DatePicker x:Name="dpEffDate" Text="{Binding Path=effectiveDate,Mode=TwoWay}"

     

    SelectedDate="{Binding Now,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type my:DataGrid}}}"

     

    DisplayDate="{Binding Now,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type my:DataGrid}}}"

     

    CalendarStyle="{DynamicResource CalenderControlTemplate}" />

     

     

    @Syed Mustehsan Ikram

    Yes it is not bound to any property. Because my datatemplate is in a resource dictionary.

    Wednesday, September 08, 2010 4:56 PM
  • Hi NewtoLinq,

    Calendar has two named Parts PART_Root and PART_Calandar which are required for templating.

    Please try the following style:

        <Style TargetType="{x:Type my:Calendar}" x:Key="CalenderControlTemplate">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="my:Calendar" >
                <Grid>
    
                  <my:CalendarItem Name="PART_CalendarItem" 
                           Background="White" 
                           BorderBrush="Black"
                           BorderThickness="1"
                           VerticalAlignment="Center" />
                </Grid>
            </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}"
                  CalendarStyle="{DynamicResource CalenderControlTemplate}" />
        </DataTemplate>
    

    If it doens't work, please leave all properties for dpEffDate to their default values and see if it works.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, September 09, 2010 7:32 AM
  • @Min

     

    I have the exact same code. But it still does not work.

    Thursday, September 09, 2010 2:05 PM
  • Hi NewtoLinq,

    Have you checked what the default date will be for <my:DatePicker />?

    I think this issue might not caused by the code snippet you have posted. Would you please share a small sample that can reproduce this issue? It will help us identify the problem.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, September 10, 2010 1:31 AM
  • I changed my Code to the following:

    <

     

    Style TargetType="{x:Type my:Calendar}" x:Key="CalenderControlTemplate">

     

     

    <Setter Property="Template">

     

     

    <Setter.Value>

     

     

    <ControlTemplate TargetType="my:Calendar" >

     

     

    <my:CalendarItem Name="PART_CalendarItem"

     

    Background="White"

     

    BorderBrush="Black"

     

    BorderThickness="1"

     

    Foreground="Black"

     

    VerticalAlignment="Center"

     

     

    />

     

     

    </ControlTemplate>

     

     

     

    </Setter.Value>

     

     

    </Setter>

     

     

    </Style>

    <my:DataGridTemplateColumn.CellTemplate>

    <

     

    DataTemplate x:Key="ContactEffDateTimePickerControl">

     

     

    <Label x:Name="lblContactEffDate" Background="Transparent" Foreground="Black" Content="{x:Static sys:DateTime.Now}" ></Label>

     

     

    </DataTemplate>

    </my:DataGridTemplateColumn.CellTemplate>

    <my:DataGridTemplateColumn.CellEditingTemplate>

     

     

     

    <DataTemplate x:Key="contactEffDate">

     

     

    <my:DatePicker x:Name="dpContactEffDate" Text="{Binding Path=contact_EffDate,Mode=TwoWay}"

     

    SelectedDate="{x:Static sys:DateTime.Now}" DisplayDate="{Binding Path=contact_EffDate,Mode=TwoWay}" IsTodayHighlighted="True"

     

    Background="Transparent" Foreground="Black" CalendarStyle="{DynamicResource CalenderControlTemplate}" DataContext="{Binding}" />

     

     

    </DataTemplate>

    </my:DataGridTemplateColumn.CellEditingTemplate>

    The datetime picker now displays the current date but the problem is that when i select a date other than the current date it defaults to current date. How can i correct this issue.

    Friday, September 10, 2010 10:03 PM
  • Hi NewtoLinq,

    Then I think it should be the binding that causes the orginal issue.

    However, you still need to data bind the SelectedDate property.

    And to resolve the original issue, you need to change the default value for your underlying data.

    I think the default DateTime value for your underlying data has been set to 1/1/0001, which causes this issue. You can change

    DateTime dateTime = new DateTime();
    

    to

    DateTime dateTime = DateTime.Now;

    It will make the DatePicker to display the current date.

    Please let me know if it doesn't resolve your problem.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, September 13, 2010 2:33 AM
  • Hi Min Zhu,

    I understand and i also found that when i bind the selected date it binds to 01/01/01.

    But how do i access my datetime picker from the data template in my resource dictionary from my C# code behind?

    Monday, September 13, 2010 2:51 AM
  • Hi NewtoLinq,

    You do not need to access the DatePicker. The reason that the selected date is 01/01/01 when bound is because it's the value for a new DateTime instance. You should set a new default value for the underlying DateTime object you are binding to.

    For example, you can do something like following.

        DateTime _contact_EffDate = DateTime.Now;
        public DateTime contact_EffDate
        {
          get
          {
            return _contact_EffDate;
          }
          set
          {
            if (value.Ticks == 0)
            {
              value = DateTime.Now;
            }
            _contact_EffDate = value;
          }
        }
    

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, September 14, 2010 3:22 AM
  • Hi Min Zhu,

    I tried the following code today and it works. I am not sure if it is a good way to do it or not. But Heyy it works.

    C# Code behind:

    Microsoft.Windows.Controls.

    DatePicker _dpEffDate;

     

     

    private void dpContactEffDateAtt1_Loaded(object sender, RoutedEventArgs e)

    {

    _dpEffDate = sender

    as Microsoft.Windows.Controls.DatePicker;

    _dpEffDate.DisplayDate =

    DateTime.Now;

    _dpEffDate.SelectedDate =

    DateTime.Now;

    }

    And my XAML to the following :

     

     

    <DataTemplate x:Key="ContactEffDateTimePickerControlAtt1">

     

     

    <Label x:Name="lblContactEffDateAtt1" Background="Transparent" Foreground="Black"

     

    Content="{Binding contact_EffDate,Mode=TwoWay}"></Label>

     

     

    </DataTemplate>

     

     

    <DataTemplate x:Key="contactEffDateAtt1">

     

     

    <my:DatePicker x:Name="dpContactEffDateAtt1"

     

    SelectedDate="{Binding Path = contact_EffDate,Mode=TwoWay}"

     

    DisplayDate="{Binding Path = contact_EffDate,Mode=TwoWay}"

     

    CalendarStyle="{DynamicResource CalenderControlTemplate}"

     

    Background="Transparent" Foreground="Black" Loaded="dpContactEffDateAtt1_Loaded" >

     

     

     

    </my:DatePicker>

     

     

    </DataTemplate>

    And it works.

    • Edited by NewtoLinq Tuesday, September 14, 2010 5:02 PM Change
    • Marked as answer by NewtoLinq Tuesday, September 14, 2010 5:02 PM
    Tuesday, September 14, 2010 4:39 PM