Home > database >  How to convert TimeSpan.FromMilliseconds to Slider Value in WPF C#
How to convert TimeSpan.FromMilliseconds to Slider Value in WPF C#

Time:08-26

In my UI, I have a slider. I use the following code to translate the slider's value into a timestamp:

Timestamp_TextBox.text = TimeSpan.FromMilliseconds(VidSlider.Value).ToString(@"hh\:mm\:ss\:ff");

I would like to do the opposite, from a Timestamp string (e.g. 00:01:34:24) I would like to convert it to the slider value.

This would not reflect in the UI but will be saved in a Class so Binding isn't an option. Thank you very much.

CodePudding user response:

If you just want the total number of milliseconds, just use Timespan.Parse(...).TotalMilliseconds.

However, you should really not be converting values to string and back again unless absolutely needed. The main reason to parse strings is when the user enters text, or you are reading some string-based serialized data.

CodePudding user response:

I agree with JonasH about you should convert the string back only in limited scenarios. Having said that, you can convert back the string to TimeSpan using the same format used to convert to string:

TimeSpan.ParseExact(Timestamp_TextBox.Text, @"hh\:mm\:ss\:ff", null);

You should keep it in your mind:

  1. Since Slider.Value is double, the precision of original value is lost when converted to string and thus converted back value will be slightly different.

  2. The string represented time must be less than 24 hours.

  3. If you let user freely edit the string in TextBox, TimeSpan.TryParseExect should be used instead.

CodePudding user response:

If I understand correctly what you need, then in my opinion the best way would be to use a converter.

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace Core2022.Converters
{
    [ValueConversion(typeof(double), typeof(TimeSpan))]
    public class DoubleToTimeSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is not double number)
            {
                try
                {
                    number = System.Convert.ToDouble(value, culture);
                }
                catch (Exception)
                {

                    return DependencyProperty.UnsetValue;
                }
            }
            return TimeSpan.FromMilliseconds(number);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is not TimeSpan span)
            {
                if (value is not string text || !TimeSpan.TryParse(text, culture, out span))
                {
                    return DependencyProperty.UnsetValue;
                }
            }
            return span.TotalMilliseconds;
        }

        public static DoubleToTimeSpanConverter Instance { get; } = new();
    }

    [MarkupExtensionReturnType(typeof(DoubleToTimeSpanConverter))]
    public class DoubleToTimeSpanExtension : MarkupExtension
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return DoubleToTimeSpanConverter.Instance;
        }
    }
}
    <StackPanel>
        <Slider x:Name="VidSlider" Minimum="0"
                Maximum="10000000" Margin="10"/>
        <TextBox x:Name="Timestamp_TextBox"
                 Text="{Binding Value, ElementName=VidSlider,
                                Converter={cnvs:DoubleToTimeSpan}}"/>
    </StackPanel>
  • Related