I have a group of components like so:
<StackPanel Orientation="Horizontal" >
<Label>Between</Label>
<DatePicker Name="dtpFrom" ></DatePicker>
<TextBox Name="timeFrom" MinWidth="60" />
<Label>and</Label>
<DatePicker Name="dtpTo"></DatePicker>
<TextBox Name="timeTo"
MinWidth="60" />
</StackPanel>
where the user can select the date and set the “From” date and time and the “To” date and time.
I want to combine these (the date portion of the DatePicker with the corresponding time portion represented within the TextBox) into a DateTime value. e.g., with:
DateTime dtFrom;
…I want to be able to do something like (pseudocode):
dtFrom = (DateTime) dtpFrom.Date + Convert.ToTime(timeFrom);
Is there a way to do this?
I would recommend handling this within your DataContext (ie: ViewModel), and binding to two separate properties. These could easily map to an underlying
DateTime.For example:
This would let you bind each control to the appropriate property, but still maintain everything within a single
DateTimeinstance.