Forum Xamarin.Forms

Changing Text Color in a ListView - XAML

crazygangcrazygang BDMember ✭✭


I have a list view in xaml that gets populated by an ObservableCollection. The ObservableCollection (lets say a) holds items that have been detected by bluetooth.

The List View binds its ItemsSource to the ObservableCollection (a) and populates the list of items detected.

Now the logic behind is when the items have been detected and added to the ObservableCollection (a), the items in the ObservableCollection (a) are sent one by one to a remote database online. If the transfer is successful, all the items in the List View will have TextColor = Blue. If some items have not been transferred (due to loss of internet connectivity), the items (in the list view) that have not been transferred but detected will be of TextColor = Red.

Can anyone enlighten me on how to have to achieve this ? If possible, provide me with a simulation that achieves the above. Below, I have managed to get a Converter that achieves something similar.

**Converter **

    public class StartsWithToColorConverter : IValueConverter, IMarkupExtension
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                if (Equals(value, null)) return "#52a4b2";
                var valueAsString = value.ToString();
                return valueAsString.StartsWith("3") ? "#FF0000" : "#52a4b2";

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                throw new NotSupportedException("Only one way bindings are supported with this converter");

            public object ProvideValue(IServiceProvider serviceProvider)
                return this;


     <ListView x:Name="listView"
               Header="" SeparatorVisibility="None">

             <Grid Padding="12">
                <Label Text="{Binding ID}" FontSize="12" FontAttributes="Bold" TextColor="{Binding UID, Converter={converters:StartsWithToColorConverter }}"/>



Thanks in advance.


Sign In or Register to comment.