转换器 --> 将日期时间转换为 HH:mm:ss 格式

本文关键字:HH mm ss 格式 转换 日期 时间 转换器 | 更新日期: 2023-09-27 18:31:34

我正在尝试显示hh:mm:ss格式,但我总是显示完整的日期。

我的转换器类/依赖项属性是否有错误/缺失?我只收到日期时间.now 显示,如何将其更改为

00:00:00?

我真正想要的是将 00:00:00 显示为初始化。稍后,它会增加槽计时器。

XAML:

<ListBox.ItemTemplate>
<DataTemplate>
    DownTime="{Binding DownTime, Converter={StaticResource  DateTimeConverter}, 
    ConverterParameter='{0:hh:mm:ss'}}
</DataTemplate>
</ListBox.ItemTemplate>

依赖属性:

public static readonly DependencyProperty DownTimeProperty =
    DependencyProperty.Register("DownTime", typeof(DateTime), typeof(RegistrationButton), new UIPropertyMetadata(DateTime.Now));
public DateTime DownTime
    {
        get { return (DateTime)GetValue(DownTimestandProperty); }
        set { SetValue(DownTimeProperty, value); }
    }

转换器类:

    [ValueConversion(typeof(DateTime), typeof(String))]
public class DateTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime dt = (DateTime)value;
        return dt;
    }
    // No need to implement converting back on a one-way binding 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string strValue = value as string;
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return DependencyProperty.UnsetValue;
    }
}

谢谢。

转换器 --> 将日期时间转换为 HH:mm:ss 格式

你可以只使用 StringFormat

<TextBlock Text="{Binding DownTime, StringFormat={}{0:"hh:mm:ss"}}" />

编辑

日期时间 表示时刻,通常表示为日期和时间。

时间跨度 表示时间间隔。

他们有许多类似的方法,尤其是添加或删除小时/分钟/秒的方法,我认为这就是您想要的这种情况。