如何使用ToString()格式化Duration?

时间:2018-11-12 15:32:39

标签: c# datetime tostring duration

当尝试使用ToString()格式化持续时间时,出现以下消息'No Overload for method 'ToString()' takes 1 arguments。我在herehere上找到了以前的线程;但是,这些线程用于可为null的DateTime,并且都建议使用myDatetime.Value.ToString(),我在下面的示例中尝试过使用该线程,但仍然收到相同的消息。

    public System.Windows.Duration? MyDuration { get; set; }

    public string MyDurationString
    {
        get => (MyDuration.HasValue) ? MyDuration.Value.ToString(@"hh\:mm\:ss.fff") : null;
        set => MyDuration = string.IsNullOrEmpty(value) ?
                TimeSpan.Zero : TimeSpan.Parse(value);
    }

1 个答案:

答案 0 :(得分:4)

我不知道这种类型,但是WPF Duration似乎没有接受参数的ToString。但是它具有属性TimeSpan,您可以使用它:

get => MyDuration.HasTimeSpan ? MyDuration.TimeSpan.ToString("hh':'mm':'ss'.'fff") : null; 
相关问题