1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- namespace OINA.Extender.WPF.Testharness
- {
- using System;
- using System.Globalization;
- using System.Windows.Data;
- using System.Windows.Markup;
- /// <summary>
- /// Converts between <see cref="TimeSpan"/> and number of milliseconds as a <see cref="string" />.
- /// </summary>
- [ValueConversion(typeof(TimeSpan), typeof(string))]
- public class TimeSpanToStringMillisecondsConverter : IValueConverter
- {
- /// <summary>
- /// Converts a <see cref="TimeSpan"/> to number of milliseconds as a <see cref="string" />.
- /// </summary>
- /// <param name="value">The value produced by the binding source.</param>
- /// <param name="targetType">The type of the binding target property.</param>
- /// <param name="parameter">The converter parameter to use.</param>
- /// <param name="culture">The culture to use in the converter.</param>
- /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- try
- {
- var timeSpan = (TimeSpan)value;
- return timeSpan.TotalMilliseconds.ToString(culture);
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// Converts number of milliseconds as a <see cref="string" /> to a <see cref="TimeSpan"/>.
- /// </summary>
- /// <param name="value">The value that is produced by the binding target.</param>
- /// <param name="targetType">The type to convert to.</param>
- /// <param name="parameter">The converter parameter to use.</param>
- /// <param name="culture">The culture to use in the converter.</param>
- /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
- 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;
- }
- }
- }
- }
|