且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将WPF日期选择器的默认日期设置为当前日期

更新时间:2022-05-17 03:27:03

除非您在 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" ...