簡體   English   中英

在IValueConverter中的Type targetType參數中放入什么

[英]What to put in the Type targetType parameter in IValueConverter

我通過代碼隱藏調用IValueConverter類,但我不確定要在Type targetType參數中放入什么。 該對象是string但是使用該對象會給我“無效的表達式術語”字符串”

我的代碼調用轉換器

secondConverter.Convert(score, string, null, CultureInfo.CurrentCulture);

轉換器類

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        TimeSpan ts = new TimeSpan(0, 0, (int)value);

        return String.Format("{0:D2}:{1:D2}:{2:D2}",
                        ts.Hours,
                        ts.Minutes,
                        ts.Seconds);
    }

您可以使用typeof(string)而不是string,但您的轉換器似乎沒有使用或驗證目標類型,因此您可以放置​​任何內容,包括null。

通常,您的轉換器至少應驗證目標類型是否為字符串,如果不是,則拋出異常。

你想要

secondConverter.Convert(score, typeof(string), null, CultureInfo.CurrentCulture);

實際上使它成為Type的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM