18

I have a DatePicker object in a DataGrid that successfully shows the date from the database or a property:

<DataGridTemplateColumn.CellTemplate>
   <DataTemplate>
      <DatePicker SelectedDate="{Binding Date, StringFormat=dd/MM/yyyy}"/>
   </DataTemplate>   
</DataGridTemplateColumn.CellTemplate>

Now I want to show the time in front of the date. This is what I tried, but it doesn't work:

<DataGridTemplateColumn.CellTemplate>
   <DataTemplate>
      <DatePicker SelectedDate="{Binding Date, StringFormat='dd/MM/yyyy HH:mm:ss'}"/>
   </DataTemplate>   
</DataGridTemplateColumn.CellTemplate>

NOTE: I do not want to do this in the code-behind.

0

3 Answers 3

12

Use the extended DateTimePicker

2
  • So I am guessing by your answer that this cannot be done using the .NET Library? Sep 10, 2014 at 12:41
  • 4
    The DatePicker control does not deal with time, only dates. So you will need to use a different control.
    – Mashton
    Sep 10, 2014 at 12:46
3

In addition to, without using other components, if you just want to display a date in the format that you want to edit it, you can do this:

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock  Text="{Binding Path=Date,StringFormat={}{0:dd/MM/yyyy}}"  VerticalAlignment="Center" HorizontalAlignment="Left">
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
2

You could do something like this:

            <DataGridTemplateColumn Header="YourDate">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <DatePicker SelectedDate="{Binding YourDate, UpdateSourceTrigger=PropertyChanged}" />
                            <TextBox Margin="2 0 0 0" Text="{Binding YourDate, UpdateSourceTrigger=PropertyChanged, StringFormat='t'}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding YourDate}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

This will show an DatePicker next to an TextBox wich are bound to the same property. The TextBox got an StringFormat to only show the date.

UpdateSourceTrigger is set to PropertyChanged, so the user can't do an conflict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.