0
votes

I have a datagrid bound to an xml file in WPF. I can not figure out how to format the date that appears in my one of my DataTextColumns. I have tried converters and 'stringformat=d' and nothing seems to work. Any ideas?

The data in the 'Birthday' text column appears as 'dd/mm/yyyy hh:mm:ss AM/PM' because that is how it is stored in the xml file.

However I want it to display as 'dd/mm/yyyy'.

Here is my xaml. This is a simple binding and does not require any code behind.

<DataGrid Name="currentCrewGrid" DataContext="{StaticResource CrewInfo}" ItemsSource="{Binding XPath=/Names/Name[@isActive\=\'True\']}" AutoGenerateColumns="False" >
 <DataGrid.Columns>
   <DataGridTextColumn IsReadOnly="True" Header="Birthday" Binding="{Binding XPath=Birthday}"/>
 </DataGrid.Columns> 
</DataGrid>
1
The date is coming in as a string, so you'll need to convert it to a valid DateTime object before using a format string on it. It may be more productive to serialize your XML to objects and bind to those instead.slugster

1 Answers

1
votes

You can use a Converter for your Birthday column, to convert the date to required format.

Binding="{Binding XPath=Birthday, Converter={StaticResource birthdayConverter}}"

public class BirthdayConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
         //Convert date to desired format.
     }

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
        // Convert back.
     }
 }