namespace OINA.Extender.WPF.Testharness
{
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
///
/// Converts between and number of milliseconds as a .
///
[ValueConversion(typeof(TimeSpan), typeof(string))]
public class TimeSpanToStringMillisecondsConverter : IValueConverter
{
///
/// Converts a to number of milliseconds as a .
///
/// The value produced by the binding source.
/// The type of the binding target property.
/// The converter parameter to use.
/// The culture to use in the converter.
/// A converted value. If the method returns null, the valid null value is used.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
var timeSpan = (TimeSpan)value;
return timeSpan.TotalMilliseconds.ToString(culture);
}
catch
{
return 0;
}
}
///
/// Converts number of milliseconds as a to a .
///
/// The value that is produced by the binding target.
/// The type to convert to.
/// The converter parameter to use.
/// The culture to use in the converter.
/// A converted value. If the method returns null, the valid null value is used.
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
var timeSpan = (string)value;
return TimeSpan.FromMilliseconds(int.Parse(timeSpan, culture));
}
catch
{
return TimeSpan.Zero;
}
}
}
}