0

I have a ObservableCollection<TimeSpan> Laps which I am databinding to a gridview. This works as expected but I need to apply a converter to set the format of the TimeSpan:

In my resources:

<utils:TimeToStringConverter x:Key="myConverter"/>

My Gridview:

<GridView HorizontalAlignment="Left" Height="278" Margin="78,220,0,0" VerticalAlignment="Top" Width="1278" ItemsSource="{Binding model.Laps}" />

I have the following converter which I want to apply on the items of a GridView / ListView in Winrt:

public class TimeToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        TimeSpan t = (TimeSpan) value;

        return t.ToString(@"hh\:dd\:ss\.fff");

    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

I can't figure out how to get the converter to work, and when I apply it on the GridView then it is looking for me to convert an Observable collection rather than just a TimeSpan item. What should I do here ?

Regards

Peter
  • 1,685
  • 3
  • 16
  • 22

2 Answers2

1

You need something like a

<GridView
    ...>
    <GridView.ItemTemplate>
        <DataTemplate>
            <TextBlock
                Text="{Binding Converter={StaticResource myConverter}}" />
        </DataTemplate>
    </GridView.ItemTemplate>
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Bingo. Thank you. I attempted this the first time, assumed it was wrong, realised that instead of giving a TimeSpan object to the Convert it was actually feeding in a string strangely. I accounted for this and it worked. – Peter Mar 26 '13 at 16:52
-1

Use the below modified line

I've just modified the item source like below

ItemsSource="{Binding model.Laps,Converter={StaticResource myConverter}}"

<GridView HorizontalAlignment="Left" Height="278" Margin="78,220,0,0" VerticalAlignment="Top" Width="1278" ItemsSource="{Binding model.Laps,Converter={StaticResource myConverter}}" />
Smaug
  • 2,625
  • 5
  • 28
  • 44