wpf - 在DataGrid列中设置DatePicker的默认值

标签 wpf xaml binding

我有一个绑定(bind)到集合的 DataGrid,其中一列类型是 DateTime。

我的xaml代码是:

<DataGridTemplateColumn Width="260" Header="OrderDate">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <DatePicker SelectedDate="{Binding OrderDate,
                                       Mode=TwoWay,
                                       UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

问题是 DatePicker 默认值为 1/1/0001。

如何设置默认值?

最佳答案

问题已通过这种方式解决(抱歉,如果问题不清楚):

在类声明中:

public DateTime OrderDate { get; set; }

将其更改为:

public DateTime? OrderDate { get; set; }

它看起来像是采用了 DateTime 默认值,解决方案是添加 null。

关于wpf - 在DataGrid列中设置DatePicker的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17546213/

相关文章:

c# - 如何覆盖 MeasureOverride 以查找 ItemsControl 的大小

c# - 值转换器显示在 Intellisense 中,但编译器无法在 C# WinRT 应用程序中找到?

c# - WPF 从 MVVM 中的代码隐藏启动动画

c# - 如何将组框锚定到左侧但允许组框在拉伸(stretch)时增长到最大宽度

c# - 在二次贝塞尔曲线路径上寻找点

c# - WinRT XAML 工具包可视化错误

wpf - XAML - 如何拥有全局 inputBindings?

wpf - 将组合框选定的值绑定(bind)到属性

c# - 如何在绑定(bind)中将 FallbackValue 设置为外部图像文件的路径?

c# - 如何从用户控件里面关闭一个窗口?