Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Changing Text Color in a ListView - XAML

crazygangcrazygang BDMember ✭✭

Hello,

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

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

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

             </Grid>
           </ViewCell>
         </DataTemplate>
       </ListView.ItemTemplate>

    </ListView>

Thanks in advance.

Answers

Sign In or Register to comment.