Files
ADP/UIShare/Converters/TimeSpanToStringConverter.cs
2026-06-05 10:57:09 +08:00

39 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace UIShare.Converters
{
public class TimeSpanToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is TimeSpan timeSpan)
{
// 如果天数大于0则显示天数
if (timeSpan.Days > 0)
{
return $"{timeSpan.Days}天{timeSpan.Hours:D2}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}";
}
else
{
// 如果不超过一天,则只显示时:分:秒
return $"{timeSpan.Hours:D2}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}";
}
}
return "00:00:00";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// 通常不需要从字符串转换回TimeSpan所以这里返回UnsetValue
return System.Windows.DependencyProperty.UnsetValue;
}
}
}