如何仅显示日期,而不显示WPF数据网格中的日期和时间? [英] How to show just the date and not date and time in a WPF datagrid?

查看:46
本文介绍了如何仅显示日期,而不显示WPF数据网格中的日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF数据网格,它绑定到一个类似于以下内容的类对象:

I have a WPF datagrid with a binding to a class object that looks like the following:

<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate }" />

该类中的属性非常简单,如下所示:

The property in the class is quite simple, as follows:

public DateTime OrderDate { get; set; }

当我填充此属性时,我只是发送 DateTime.Date (仅日期,而不是时间):

When I'm populating this property I'm just sending DateTime.Date (date only, not time):

.Select(
    r =>
    new Customer
        {
            Persona = r.Persona,
            CustomerName = r.CustomerName,
            Email = r.Email,
            Organization = r.Organization,
            PhoneNumber = r.PhoneNumber,
            Street = r.Street,
            Suburb = r.Suburb,
            Postcode = r.Postcode,
            OrderDate = r.OrderDate.Date
        }))

不幸的是,这并没有在我的WPF数据网格中反映出来,因为它显示了日期,但随后它还在所有时间将时间显示为 12:00:00 AM .我应该如何只让我的WPF数据网格仅显示日期?

Unfortunately this doesn't reflect in my WPF datagrid as that's showing the date, but then it's also showing the time (everywhere) as 12:00:00AM. How should I approach just having my WPF datagrid show the date only?

推荐答案

尝试一下

<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate , StringFormat=d}" />

有关字符串格式的更多信息,此处

more info on string formattings here

这篇关于如何仅显示日期,而不显示WPF数据网格中的日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆