Can custom text be added to a listview cell?

250 views Asked by At

In my xaml form i have the following:

<ListView x:Name="listView" ItemsSource="{Binding Path=AllResults}"  ItemSelected="OnEmployeeListItemSelected" HorizontalOptions="StartAndExpand" HasUnevenRows="true">
            <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <StackLayout VerticalOptions="StartAndExpand">
                                    <Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" Font="{StaticResource fontL}" TextColor="{Binding TestResult, Converter={StaticResource resultMapper}}" />
                                    <Label Text="{Binding Path=StartedOn, StringFormat='dd-MM-yyy @ HH:mm:ss.fff'}" Font="Medium" />
                                    <Label Text="Test went on for {Binding NoOfHours}" Font="Medium"/>
                                </StackLayout>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
</ListView>

AllResults, which is the binding for the listview is a List type that contains a ORM based objects from the DB.

public class Result
    {
        [PrimaryKey, AutoIncrement]
        public int TestID { get; set; }
        public DateTime StartedOn { get; set; }
        public DateTime EndedOn { get; set; }
        public TimeSpan NoOfHours{ get; set; }
        public string TestResult{ get; set; }

        public Result ()
        {
            StartedOn = DateTime.Now;
            NoOfHours = TimeSpan.Zero;
            TestResult = "";
        }
    }

I need help for two things:

  1. For the date I tried to use the method mentioned here, but it just displays as "dd-MM-yyy @ HH:mm:ss.fff"
  2. To show "Test went for of x", where x is a Time of hours
2

There are 2 answers

1
Yegor On
  1. The property NoOfHours is of the TimeSpan type which uses different format specifiers.
  2. To append a string to the binding, use the following syntax: <Label Text="{Binding NoOfHours, StringFormat=Test went on for: {0:hh}}"/>
0
alt-ctrl-dev On

I solved this by using convertor as mentioned here. This was more flexible and easier to implement