问题
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; }
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
}))
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?
回答1:
Try this
<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate , StringFormat=d}" />
more info on string formattings here
回答2:
Alternately you can do it in the code:
((DataGridTextColumn)YourGridName.Columns[0]).Binding.StringFormat = "d";
(Credit: I got his information from Mark Feldman's answer to this question: Date formatting in WPF datagrid)
来源:https://stackoverflow.com/questions/32131208/how-to-show-just-the-date-and-not-date-and-time-in-a-wpf-datagrid