我有一个UserControl。我使用MaskedText框从WPF ToolKit输入时间值。 在xaml:
<StackPanel Margin="5" Orientation="Horizontal"
Grid.Row="0" Grid.Column="0">
<CheckBox Name="ckMon" Click="ckMon_Click">MON</CheckBox>
<CheckBox Name="ckTue" Click="ckTue_Click">TUE</CheckBox>
<CheckBox Name="ckWed" Click="ckWed_Click">WED</CheckBox>
<CheckBox Name="ckThu" Click="ckThu_Click">THU</CheckBox>
<CheckBox Name="ckFri" Click="ckFri_Click">FRI</CheckBox>
<CheckBox Name="ckSat" Click="ckSat_Click">SAT</CheckBox>
<CheckBox Name="ckSum" Click="ckSum_Click">SUN</CheckBox>
</StackPanel>
<msk:MaskedTextBox Grid.Row="0" Grid.Column="1"
Name="txt_starttime"
Mask="00:00"
TextChanged="txt_totalhours_TextChanged"
Text="{Binding Path=FromTime}"/>
<msk:MaskedTextBox Grid.Row="0" Grid.Column="2"
Name="txt_endtime"
Mask="00:00"
TextChanged="txt_totalhours_TextChanged"
Text="{Binding Path=ToTime}"/>
<TextBox Grid.Row="0" Grid.Column="3"
Name="txt_totalhours"
TextChanged="txt_totalhours_TextChanged"
Text="{Binding Path=TotalHours}"/>
CS:
private TimeSpan FindTimeSpan(string from, string to)
{
var fromTime = DateTime.MinValue;
var toTime = DateTime.MinValue;
var TimeTrue = DateTime.MaxValue;
string strnew = string.Empty;
if (DateTime.TryParse(from, out fromTime))
{
if (DateTime.TryParse(to, out toTime))
{
if (toTime > fromTime)
{
return toTime.Subtract(fromTime);
}
else
{
TimeSpan spantarget = TimeSpan.Parse("23:59");
TimeSpan spanfrom = TimeSpan.Parse(from);
TimeSpan expectspam = TimeSpan.Parse(to);
DateTime d1 = DateTime.MinValue.Add(spantarget);
DateTime d2 = DateTime.MinValue.Add(spanfrom);
var CollectTime = d1.Subtract(d2);
var sR = CollectTime.Add(expectspam);
return CollectTime.Add(expectspam);
}
}
}
return new TimeSpan();
}
private void txt_totalhours_TextChanged(object sender, TextChangedEventArgs e)
{
if (null == txt_totalhours || null == txt_starttime || null == txt_endtime)
return;
if (-1 < txt_starttime.Text.IndexOf("_") || -1 > txt_endtime.Text.IndexOf("_"))
{
return;
}
TimeSpan span = FindTimeSpan(txt_starttime.Text, txt_endtime.Text);
span = TimeSpan.FromTicks(span.Ticks * ck_count);
string time = span.TotalHours.ToString();
txt_totalhours.Text = Math.Round(Convert.ToDecimal(time), 2).ToString();
CurrentHVACUsageHour.TotalHours = Math.Round(Convert.ToDecimal(time), 2).ToString();
return;
}
根据工作日的决定,我从“FromTime”中减去“ToTime”并将其乘以工作日选择。我有效地做到了这一点。
我计划用于触摸屏排序设备。所以Timer控件应该全部组合并且易于使用。
新时间控制中需要的相同计算。真正重视任何帮助。非常感谢提前。