简体   繁体   中英

ValueConverter method is hit but not showing on UI

I a have list containing invoices. These invoices have a direction value represented by an int (incoming = 0, issued = 1). I want to differentiate these invoices by color in the list.

I tried to use ValueConverter, but when I populate the list with invoices, it's not working and background color stays default color, even though the ValueConvert method is hit when I place breakpoint in it and it revives all the objects from the list. If I replace the bounding to any color in the xaml file, it works and background changes to the specific color.

What am I missing here?

My xaml (only relevant code included):

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

And my 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, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

As Jason mentioned in his comment, you should return a Color type instead of a Brush type.

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, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM