1

I am binding a Listbox with a List which contains Minutes(200), how do I display relative time, like:

  • 2 hours ago
  • 3 days ago
  • 1 month ago

    <ListBox x:Name="listBox">
     <ListBox.ItemTemplate>
       <DataTemplate >
    
         <TextBlock x:Name="TxtDuration" Text="{Binding Duration}"/>
    
      </DataTemplate>
    </ListBox.ItemTemplate>
    

Veejay
  • 89
  • 1
  • 7

1 Answers1

2

You need something called a relative time converter.

If you search online, you'll find plenty of different implementations. A good one is definitely a part of Callisto toolkit.

You can also check out similar questions on StackOverflow such as this one.

Using a converter is really straightforward.

<TextBlock x:Name="TxtDuration" Text="{Binding Duration, Converter={StaticResource RelativeTimeConverter}}"/>

In this case the RelativeTimeConverter is declared as a static resource somewhere in your app.

Community
  • 1
  • 1
Igor Ralic
  • 14,975
  • 4
  • 43
  • 51
  • Thanks for your reply, i tried to use the Callisto RelativeTimeConverter but it's used for only Windows 8 Metro apps the namespace which are used in this converter are not available in windows 8 phone apps, please suggest using Windows.ApplicationModel.Resources; using Windows.ApplicationModel.Resources.Core; using Windows.UI.Xaml.Data; – Veejay Jul 03 '14 at 09:35
  • Also RelativeTimeConverter takes input in dateformat and i have input minutes ex.(200) – Veejay Jul 03 '14 at 09:39
  • After searching internet i found this article and it's giving turnaround solution http://www.geekchamp.com/articles/windows-phone-toolkit-datetime-converters But RelativeTimeConverter takes input in Dateformat and i have input minutes ex.(200) how can i convert it in dateformat before feeding into RelativeTimeConverter, please suggest – Veejay Jul 03 '14 at 09:51
  • @user2717050 Depending on what exactly are those minutes? Minutes relative to what? So, let's say there are 100 minutes. Does that mean that something happened 100 minutes before this very moment or? – Igor Ralic Jul 03 '14 at 11:25
  • I made some changes in the Microsoft.Phone.Controls.ToolKit RelativeTimeConverter and it will do the required task public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DateTime objDateTime= DateTime.Now.Date.AddMinutes(System.Convert.ToDouble(value.ToString())); value = objDateTime; } – Veejay Jul 03 '14 at 11:40
  • @user2717050 great. just be careful with licensing issues when using code you find online. and don't forget to upvote/mark as answer my answer if it helped you out. – Igor Ralic Jul 03 '14 at 11:46