繁体   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