-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMillisecondToTimeSpanConverter.cs
More file actions
49 lines (44 loc) · 1.7 KB
/
MillisecondToTimeSpanConverter.cs
File metadata and controls
49 lines (44 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace Demo
{
using System;
using System.Windows.Data;
/// <summary>
/// Converts from a TimeSpane input value -> string form of the TimeSpan in milliseconds, and vice-versa.
/// </summary>
internal sealed class MillisecondToTimeSpanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TimeSpan timeSpan = (TimeSpan)value;
if (timeSpan != null)
{
return ((int)timeSpan.TotalMilliseconds).ToString();
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = (string)value;
if (input != null)
{
int parsedInt;
if (Int32.TryParse(input, out parsedInt))
{
//no setting times over a minute for animations ;)
if (parsedInt <= 60000)
{
int fullSeconds = parsedInt / 1000;
double milliseconds = ((double)(parsedInt % 1000)) / 1000;
TimeSpan result;
string parseString = String.Format("0:0:{0}{1:f.0}", (fullSeconds != 0) ? fullSeconds.ToString() + "." : String.Empty, milliseconds.ToString());
if (TimeSpan.TryParse(parseString, out result))
{
return result;
}
}
}
}
return Binding.DoNothing;
}
}
}