在我的 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 的绑定(bind),是一个列表类型,包含来自数据库的基于 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 = "";
}
}
我需要两件事的帮助:
- 对于日期,我尝试使用提到的方法 here , 但它只显示为 "dd-MM-yyy @ HH:mm:ss.fff"
- 显示“测试进行了 x”,其中 x 是小时数
最佳答案
- 属性(property)
NoOfHours
属于TimeSpan
使用 different 的类型格式说明符。 - 要将字符串附加到绑定(bind),请使用以下语法:
<Label Text="{Binding NoOfHours, StringFormat=Test went on for: {0:hh}}"/>
关于c# - 可以将自定义文本添加到 ListView 单元格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30702426/