我想以特定格式在TextBlock我的变量TrainDelay中显示

我使用Converter IntToTimeSpanConverter格式化TrainDelay:(mm:ss)

因此根据TrainDelay的值,例如:


以红色显示Delayed (00:23)
以深色显示On Time (00:00)
以绿色显示In Advance (- 00:15)


一些代码:

public class TimeSpanFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int.TryParse(value.ToString(), out int time);
        value = TimeSpan.FromSeconds(time);
        if (string.IsNullOrWhiteSpace(value.ToString()) || ((TimeSpan)value).Equals(TimeSpan.MinValue))
            return "––:––";
        else if(time > 0)
        {
            return TrainDelay.Delayed + "  " + ((((TimeSpan)value) < TimeSpan.Zero) ? "-" : "") + ((TimeSpan)value).ToString(@"mm\:ss");
        }
        else if (time < 0)
        {
            return TrainDelay.InAdvance + "  " + ((((TimeSpan)value) < TimeSpan.Zero) ? "-" : "") + ((TimeSpan)value).ToString(@"mm\:ss");
        }
        else
        {
            return TrainDelay.OnTime + "  " + ((((TimeSpan)value) < TimeSpan.Zero) ? "-" : "") + ((TimeSpan)value).ToString(@"mm\:ss");
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public enum TrainDelay
{
    OnTime,
    Delayed,
    InAdvance
}


我已经在XAML中使用DataTrigger进行了尝试:

<TextBlock Name="tb" >
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="defaultDelay"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=tb, Path=TrainDelay, Converter={StaticResource TimeSpanFormatConverter}}" Value="Delayed">
                    <Setter  Property="Foreground" Value="Red"/>
                    <Setter Property="Text" Value="{Binding TrainDelay, Converter={StaticResource TimeSpanFormatConverter}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>


我仍然没有正确的结果!

我是C#WPF编程的初学者,我需要实现此帮助,或者可能需要更多说明才能真正理解问题

最佳答案

该绑定的作用是找到名为tb的元素,然后在该元素上查找名为TrainDelay的属性。就在那儿失败了,因为TextBlock没有该名称的属性。 TrainDelay是视图模型的属性,而不是控件的属性。

<DataTrigger
    Binding="{Binding ElementName=tb, Path=TrainDelay, Converter={StaticResource TimeSpanFormatConverter}}"
    Value="Delayed">


如果要触发火车的“延迟”,则需要另一个转换器将TrainDelay属性转换为枚举。将“延迟”与格式化的时间字符串进行比较将永远无法进行。

该新转换器看起来像这样。这只是另一个的简化版本。在我讨论它的同时,我将重写您的转换器以简化它并删除大量冗余代码。冗余代码不是一个好主意:起初,它只是混乱。然后,一年后有人来维护该东西,他们浪费了一些时间来确认这三个子表达式毕竟是完全相同的。明年,其他人将更换其中两个。一年之后,有人需要进行另一次更改,并错误地认为第三个应该有所不同。同时,其他人复制并粘贴了整个混乱,现在您遇到了更多问题。

public class TimeToDelayConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int.TryParse(value.ToString(), out int time);
        var span = TimeSpan.FromSeconds(time);

        if (string.IsNullOrWhiteSpace(value.ToString()) || span.Equals(TimeSpan.MinValue))
        {
            return null;
        }
        else
        {
            return TimeSpanFormatConverter.SecondsToDelay(time);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class TimeSpanFormatConverter : IValueConverter
{
    public static TrainDelay SecondsToDelay(int time)
    {
        if (time > 0)
        {
            return TrainDelay.Delayed;
        }
        else if (time < 0)
        {
            return TrainDelay.InAdvance;
        }
        else
        {
            return TrainDelay.OnTime;
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int.TryParse(value.ToString(), out int time);

        //  Don't assign this back to value. Assign it to a new variable that's properly
        //  typed, so you don't need to cast it.
        var span = TimeSpan.FromSeconds(time);

        if (string.IsNullOrWhiteSpace(value.ToString()) || span.Equals(TimeSpan.MinValue))
        {
            return "––:––";
        }
        else
        {
            //  No need to copy and paste the same code three times.
            var timeStr = ((span < TimeSpan.Zero) ? "-" : "") + span.ToString(@"mm\:ss");
            return $"{SecondsToDelay(time)}  {timeStr}";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
public enum TrainDelay
{
    OnTime,
    Delayed,
    InAdvance
}


然后您的触发器看起来像这样。请注意,它使用的转换器与以前不同。

<DataTrigger
    Binding="{Binding TrainDelay, Converter={StaticResource TimeToDelayConverter}}"
    Value="Delayed">

09-07 03:56