可以将自定义文本添加到列表视图单元格吗?

时间:2015-06-08 06:23:25

标签: c# xaml listview xamarin

在我的xaml表格中,我有以下内容:

<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是listview的绑定,它是一个List类型,包含来自DB的基于ORM的对象。

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 = "";
        }
    }

我需要帮助两件事:

  1. 关于我尝试使用here方法的日期,它只显示为&#34; dd-MM-yyy @HH:mm:ss.fff&#34;
  2. 显示&#34;测试用于x&#34;,其中x是时间小时

2 个答案:

答案 0 :(得分:0)

  1. 属性NoOfHours属于使用different格式说明符的TimeSpan类型。
  2. 要将字符串附加到绑定,请使用以下语法:<Label Text="{Binding NoOfHours, StringFormat=Test went on for: {0:hh}}"/>

答案 1 :(得分:0)

我通过使用转换器解决了这个问题here。这更灵活,更容易实施