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 に答える