I have a DateTime
property where I want to save the Date and the Time of something. Therefore I made a DatePicker
in WPF
where I can choose the Date. Next to it I put a Combobox
with some time entries.
Now I want to join the time which I selected in the combobox
with the DateTime
of the DatePicker
so that the Default value of 00:00:00
gets replaced by my value I selected in the comboBox
. How do I do that?
Here some code snippets. I fill my combo boxes like that:
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 2; j++)
{
string minute;
if (j == 0)
{
minute = "00";
}
else
{
minute = "30";
}
//view.cmbStartZeit.Items.Add(i.ToString() + ":" + j.ToString());
//startZeit.Content = i.ToString() + ":" + minute;
//endZeit.Content = i.ToString() + ":" + minute;
StartTime.Items.Add(i.ToString("00") + ":" + minute);
EndTime.Items.Add(i.ToString("00") + ":" + minute);
}
StartTime and EndTime are my ComboBoxes
I want to Join my DatePicker
and the time in the combo boxes. I found a Code Sample but It does not work.
((DateTime)view.StartDate.SelectedDate).Add(TimeSpan.FromMinutes(Convert.ToDouble((view.StartTime.SelectedValue.ToString()).Split(':')[0]))).Add(TimeSpan.FromHours(Convert.ToDouble((view.StartTime.SelectedValue.ToString()).Split(':')[1])));
StartDate
is my DatePicker
. It always gives me null at StartTime.SelectedValue.ToString()
If somebody knows another way to do that, without using third party kits or stuff like that please tell me. I have to use the native WPF
stuff (it's for school)
Many thanks in advance
B.Pumpkin
Running a similar example works fine for getting StartTime.SelectedValue.ToString()
.
There are other problems though:
DateTime.Add is a non mutating method, and just returns a new DateTime object. So first thing you need to do is set the new DateTime to your StartDate object. Also you are parsing minutes, and hours in opposite order (but there is a better way to parse it, see below).
Here is what I did to make it work:
var time = TimeSpan.Parse(startTime.SelectedValue.ToString());
startDate.SelectedDate = ((DateTime)startDate.SelectedDate).Add(time);
Full (simple), working example:
<StackPanel>
<DatePicker Name="startDate" />
<ComboBox Name="startTime" SelectionChanged="StartTime_OnSelected" />
</StackPanel>
Code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 2; j++)
{
string minute;
if (j == 0)
{
minute = "00";
}
else
{
minute = "30";
}
//view.cmbStartZeit.Items.Add(i.ToString() + ":" + j.ToString());
//startZeit.Content = i.ToString() + ":" + minute;
//endZeit.Content = i.ToString() + ":" + minute;
startTime.Items.Add(i.ToString("00") + ":" + minute);
}
}
}
private void StartTime_OnSelected(object sender, RoutedEventArgs e)
{
var time = TimeSpan.Parse(startTime.SelectedValue.ToString());
// startDate.SelectedDate could be null, so do some checking before
startDate.SelectedDate = ((DateTime)startDate.SelectedDate).Add(time);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.