You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin Forms中设置ListView描述显示两行并加省略号

解决方案

其实这个需求不用复杂的自定义逻辑,直接利用Xamarin.Forms Label的内置属性就能轻松搞定!

你只需要给绑定Description的Label添加两个关键属性:

  • Lines="2":强制Label最多显示2行内容
  • LineBreakMode="TailTruncation":当文本超出2行时,自动在末尾添加省略号(...)截断

修改后的完整代码如下:

<ListView x:Name="OrderView" RowHeight="70" ItemSelected="ItemTapped" BackgroundColor="#ffffff">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell Height="70">
                <ContentView Padding="10,10,10,10">
                    <StackLayout Spacing="0">
                        <Label Text="{Binding FullPathName}" TextColor="Black" FontSize="Small" HorizontalOptions="StartAndExpand" HeightRequest="20" />
                        <!-- 修改后的Description Label -->
                        <Label Text="{Binding Description}" 
                               TextColor="Gray" 
                               FontSize="Micro" 
                               HorizontalOptions="StartAndExpand" 
                               HeightRequest="30"
                               Lines="2"
                               LineBreakMode="TailTruncation"/>
                    </StackLayout>
                </ContentView>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

再补充两句细节说明:

  • Lines属性直接控制Label的最大显示行数,设为2刚好匹配你要显示前两行的需求
  • LineBreakMode的TailTruncation是专门针对文本溢出的末尾截断方案,还有HeadTruncation(开头截断)、MiddleTruncation(中间截断)等模式,你的场景用TailTruncation是最贴合的

内容的提问来源于stack exchange,提问作者Ranjit Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:33:46