c# - 可以将自定义文本添加到 ListView 单元格吗?

标签 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 的绑定(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 = "";
        }
    }

我需要两件事的帮助:

  1. 对于日期,我尝试使用提到的方法 here , 但它只显示为 "dd-MM-yyy @ HH:mm:ss.fff"
  2. 显示“测试进行了 x”,其中 x 是小时数

最佳答案

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

关于c# - 可以将自定义文本添加到 ListView 单元格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30702426/

相关文章:

c# - 尝试编写我自己的日期程序,需要检查 C# 中指定月份的最大日期

c# - 类特定常量是否应该包含在嵌套类中?

c# - NET中的SocketAsyncEventArgs和线程安全

c# - 使用静态可见性枚举作为命令参数

c# - 在 XAML 中设置命令目标

asp.net - 显示带有嵌套 ListViews 的 IGrouping<>

c# 递归反射和通用列表设置默认属性

c# - 如何扩展自定义解决方案 Xamarin 的按钮

android - onListItemClick() 方法不起作用

android - Listview - 屏幕底部的页脚