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

.NET MAUI中如何修改TextCell内文本的字号?

解决.NET MAUI ListView中TextCell无法设置字号的问题

TextCell本身确实没有FontSize属性——它是为快速创建极简文本单元格设计的,样式定制能力有限。要修改文本字号,你可以用ViewCell自定义单元格内容,通过Label来实现字号控制,具体修改如下:

修改后的XAML代码:

<ListView x:Name="Persons"
          Grid.Column="0"
          Grid.Row="1"
          ItemSelected="Persons_ItemSelected" 
          Margin="10,10,10,0"
          RowHeight="40"
          BackgroundColor="LightGray">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding Name}" 
                       FontSize="16"  <!-- 这里设置你需要的字号数值 -->
                       VerticalOptions="Center"
                       Margin="15,0"/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

补充说明:

  • ViewCell支持完全自定义单元格的布局与样式,除了字号,还能调整字体颜色、字重、内边距等更多属性。
  • 如果需要统一所有列表项的样式,可创建Style资源复用,避免重复代码:
<ContentPage.Resources>
    <Style TargetType="Label" x:Key="ListItemLabel">
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="VerticalOptions" Value="Center"/>
        <Setter Property="Margin" Value="15,0"/>
    </Style>
</ContentPage.Resources>

<!-- 在ViewCell中引用样式 -->
<Label Text="{Binding Name}" Style="{StaticResource ListItemLabel}"/>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 20:52:47