ValueConverter 方法已命中但未在 UI 上显示

问题描述

我有一份包含发票的清单。这些发票具有由 int 表示的方向值(传入 = 0,发出 = 1)。我想在列表中按颜色区分这些发票。

我尝试使用 ValueConverter,但是当我用发票填充列表时,它不起作用并且背景颜色保持认颜色,即使当我在其中放置断点时命中了 ValueConvert 方法并且它恢复了列表中的所有对象.如果我将边界替换为 xaml 文件中的任何颜色,它就会起作用并且背景会更改为特定颜色。

在这里遗漏了什么?

我的 xaml(仅包含相关代码):

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyApp.Views.InvoiceNumberPage"
         xmlns:local="clr-namespace:MyApp.viewmodels"
         xmlns:converters="clr-namespace:MyApp.Helpers">
<ContentPage.Resources>
    <ResourceDictionary>
        <converters:InvoiceDirectionColorConverter x:Key="InvoiceDirectionColorConverter"/>
    </ResourceDictionary> 

<ListView Grid.Row="4" Grid.ColumnSpan="2" ItemsSource="{Binding FoundList}" Margin="20,0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid BackgroundColor="{Binding Direction,Converter={StaticResource InvoiceDirectionColorConverter}}">
                            <Grid.RowDeFinitions>
                                <RowDeFinition />
                            </Grid.RowDeFinitions>
                            <Grid.ColumnDeFinitions>
                                <ColumnDeFinition Width="3*"/>
                                <ColumnDeFinition Width="*"/>
                            </Grid.ColumnDeFinitions>
                            <Label Text="{Binding InvoiceNumber}" Grid.Row="0" Grid.Column="0"/>
                            <Label Text="{Binding IssueDate,StringFormat='{0:yyyy.MM.dd}'}" Grid.Row="0" Grid.Column="1"/>
                            <Label Text="{Binding Company.Name}" Grid.Row="1" Grid.Column="0"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

还有我的 ValueConverter:

class InvoiceDirectionColorConverter : IValueConverter
{
    public object Convert(object value,Type targettype,object parameter,CultureInfo culture)
    {
        SolidColorBrush brush = new SolidColorBrush();
        int invoicedirection = (int)value;
        if (invoicedirection != null && invoicedirection.Equals(0))
        {
            brush.Color = Color.Green;
        }
        else
        {
            brush.Color = Color.Yellow;
        }
        return brush;
    }

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

解决方法

正如 Jason 在他的评论中提到的,您应该返回 Color 类型而不是 Brush 类型。

class InvoiceDirectionColorConverter : IValueConverter
{
    public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
    {
        int invoicedirection = (int)value;
        if (invoicedirection != null && invoicedirection.Equals(0))
        {
            return Color.Green;
        }
        else
        {
            return Color.Yellow;
        }
    }

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