簡體   English   中英

帶綁定的TextBlock的條件語句

[英]Conditional statement on TextBlock with Binding

我正在嘗試使一個InfoPanel顯示與火車的延誤有關的一些信息。

我有一個Int變量TrainDelay ,該變量由轉換器TimeSpanFormatConverter轉換,我想根據使用綁定的TrainDelay值更改TextBlock顯示的Text。

這是我要執行的條件語句:

    if TrainDelay > 0  display "Delayed" 
    if TrainDelay < 0  display "In Advance" 
    if TrainDelay = 0  display "On Time"

TimeSpanFormatConverter:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    int time = int.Parse(value.ToString());
    value = TimeSpan.FromSeconds(time);
    if (string.IsNullOrWhiteSpace(value.ToString()) || ((TimeSpan)value).Equals(TimeSpan.MinValue))
        return "––:––";
    else
        return ((((TimeSpan)value) < TimeSpan.Zero) ? "-" : "") + ((TimeSpan)value).ToString(@"mm\:ss");
}

XAML:

<TextBlock  Text="{Binding TrainDelay, Converter={StaticResource TimeSpanFormatConverter}}"/>

我該如何實施。

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    int time = System.Convert.ToInt32(value);

    if (time > 0)
    {
        return "Delayed";
    } 

    if (time < 0 )
    {
        return "In Advance";
    }

    if (time == 0)
    {
        return "On Time";
    }

    return ""; //Or Argument Exception, your call
}

暫無
暫無

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

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