Search code examples
c#windows-phone-7toolkit

Using WP7 Date & Time Converters in the code behind file


currently I have this in my XAML:

<phone:PhoneApplicationPage.Resources>
    <toolkit:RelativeTimeConverter x:Key="RelativeTimeConverter"/>
</phone:PhoneApplicationPage.Resources>

....

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

But I don't want to bind it to the textbox, instead I want to use it in the code behind file. Any ideas how to do that?

Based on this: http://www.jeff.wilcox.name/2011/08/august2011phonetoolkit/


Solution

  • Like this:

    var converter = new RelativeTimeConverter();
    var relativeTime = (string)converter.Convert(e.Result.DateTime,null,null,new System.Globalization.CultureInfo("en-US"));
    
    txtTimeAdded.Text = relativeTime;
    

    (If you're using the other fields, you should pass them properly. But from your Binding, it doesn't appear that you are.)