Set the Default Date of WPF Date Picker to Current Date

后端 未结 3 957
情话喂你
情话喂你 2021-02-19 21:12

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

So i have used a DataTemplateColumn as Follows



        
相关标签:
3条回答
  • 2021-02-19 21:27

    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" ...
    
    0 讨论(0)
  • 2021-02-19 21:28

    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.

    0 讨论(0)
  • 2021-02-19 21:32

    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:

     get
     {
      return (ClassDate - DateTime.MinValue).TotalDays == 0 ? DateTime.Now :ClassDate;
     }
    

    cheers :)

    0 讨论(0)
提交回复
热议问题