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

编程入门 行业动态 更新时间:2024-10-28 00:14:53
本文介绍了将WPF日期选择器的默认日期设置为当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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日期选择器的默认日期设置为当前日期

本文发布于:2023-11-28 14:49:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   设置为   选择器   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!