0

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

我的转换器类/依赖属性有什么问题/缺少吗?我只收到 Datetime.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;
    }
}

谢谢。

4

1 回答 1

4

你可以只使用 StringFormat

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

编辑

DateTime表示时间的瞬间,通常表示为日期和时间。

TimeSpan表示时间间隔。

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

于 2012-04-19T14:51:54.027 回答