Andreas
Andreas

Reputation: 1479

RSS-reader that keeps unread items

So I have a simple RSS-reader, that has a feed that gets updated when the app is started. How can I add functionality that keeps the new unread items in a different color? I would like to make it visible for the user which posts are new since last time he/she opened the app.

Upvotes: 1

Views: 138

Answers (1)

MrMDavidson
MrMDavidson

Reputation: 2735

Presuming you have a model something like;

public class RSSItem {
  public bool IsUnread { get; set; }
  public string Title { get; set; }
}

You'll want to bind the ForegroundColor of a TextBlock to your IsUnread property using a IValueConverter that takes a bool and returns a Color. So your XAML might look like;

<phone:PhoneApplicationPage.Resources>
  <converters:UnreadForegroundConverter x:Key="UnreadForegroundConverter" />
</phone:PhoneApplicationPage.Resources>

<ListBox x:Name="RSSItems">
  <DataTemplate>
    <TextBlock Text="{Binding Title}" Foreground="{Binding IsUnread, Converter={StaticResource UnreadForegroundConverter}}" />
  </DataTemplate>
</ListBox>

Don't forget to add the xmlns:converters attribute to your Page's tag.

You'll then want to implement your IValueConverter to do the boolean to colour conversion;

public class UnreadForegroundConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    if ((bool)value == true) {
      return Application.Current.Resources["PhoneAccentColor"];
    }

    return Application.Current.Resources["PhoneForegroundColor"];
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    throw new NotImplementedException();
  }

}

And obviously you'll need to bind the listbox, RSSItems, to a collection of RSSItem. Eg.

ObservableCollection<RSSItem> items = new ObservableCollection<RSSItem>();
// populate items somehow
RSSItems.ItemsSource = items;

Hope that helps.

Upvotes: 3

Related Questions