Listview UWP彩色前景列表视图条件

Listview UWP彩色前景列表视图条件,listview,colors,uwp,textblock,foreground,Listview,Colors,Uwp,Textblock,Foreground,我想知道是否有可能在ListView中的textblock上设置条件。我解释: 我有一个包含一些数据的模型,这个模型中有一个“数量”。如果数量是负数,我想把前景变成红色,如果是正数,我想把前景变成绿色 <TextBlock RelativePanel.AlignRightWithPanel="True" Foreground="Red" FontWeight="Bo

我想知道是否有可能在ListView中的textblock上设置条件。我解释:

我有一个包含一些数据的模型,这个模型中有一个“数量”。如果数量是负数,我想把前景变成红色,如果是正数,我想把前景变成绿色

 <TextBlock RelativePanel.AlignRightWithPanel="True"
                               Foreground="Red"
                               FontWeight="Bold">
                        <Run Text="{Binding Amount}" />
                        <Run Text="€" />
 </TextBlock>

这是文本块,他在ListView.ItemTemplate中

问候,


安东尼

你应该使用转换器。创建一个从
IValueConverter
派生的类(例如
AmountColorConverter

公共对象转换(对象值,…)
{
var val=(双)值;
返回值>=0
?颜色。绿色
:颜色。红色;
}
一旦实现,在XAML中创建转换器实例并在绑定中引用它:


我已经试过了。 他使用我的xaml代码:

<TextBlock HorizontalAlignment="Right"
           Grid.Column="2"
           Grid.Row="0"
           Foreground="{Binding Amount, Mode=TwoWay, Converter={StaticResource ForegroundColorAmount}}"
           FontWeight="Medium">
               <Run Text="{Binding Amount}" Foreground="{Binding Amount, Mode=TwoWay, Converter={StaticResource ForegroundColorAmount}}" />
               <Run Text="€" />
下面是我的转换器类:

    public class ForegroundColorAmount : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var val = (double)value;
        if (val >= 0)
        {
            return Colors.Green;
        }
        else
        {
            return Colors.Red;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
谢谢


安东尼

非常感谢你的回复,我做了,但没有改变前景的颜色。我尝试在调试和颜色返回是正常的,但没有改变颜色。。。所以weird@AnthonyNfr该类还有一个
前台
属性。可能转换器应用于
TextBlock
的值会被
Run
实例的值覆盖。
    public class ForegroundColorAmount : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var val = (double)value;
        if (val >= 0)
        {
            return Colors.Green;
        }
        else
        {
            return Colors.Red;
        }
    }

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