【问题标题】:Can custom text be added to a listview cell?可以将自定义文本添加到列表视图单元格吗?
【发布时间】:2015-06-08 06:23:24
【问题描述】:

在我的 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 是列表视图的绑定,它是一个列表类型,其中包含来自数据库的基于 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. 显示“Test went for of x”,其中 x 是 Time of hours

【问题讨论】:

    标签: c# xaml listview xamarin


    【解决方案1】:
    1. 属性NoOfHours 属于TimeSpan 类型,它使用different 格式说明符。
    2. 要将字符串附加到绑定,请使用以下语法:&lt;Label Text="{Binding NoOfHours, StringFormat=Test went on for: {0:hh}}"/&gt;

    【讨论】:

    • 嗨@Yegor,对于数字1,它是一个日期时间。我尝试使用 StringFormat=Started on: {0:MM/dd/yy H:mm:ss}。但仍然没有工作。此外,在这两种情况下,我都会不断收到“输入字符串格式不正确”的错误。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2018-10-11
      • 2018-12-29
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多