如何在Xamarin.UWP中的TableView TableSection中更改TextSize和Casing

问题描述

我有一个用Xamarin CustomTableView制作的TableView控件。我使用自定义渲染器,以受此post启发的代码来更改Android和iOS的TextSize和Boldness。

我想对UWP做同样的事情,但是我不知道如何实现。具体来说,我想使TableSections中的Text更大,并使该文本也以大写字母开头。任何有关如何实现此目标的想法将不胜感激。

解决方法

如何在Xamarin.UWP的TableView TableSection中更改TextSize和大小写

请检查此code line,Xamarin将TextBlock放置在TableSection DataTemplate中。如果要编辑属性,可以通过在UWP项目中的DataTemplate文件中添加App.Xaml来实现。如果要编辑FontSize,可以使用下面的代码。 (请注意FontSize属性)

<Application.Resources>
    <DataTemplate x:Key="TableSectionOne">
        <TextBlock
            Margin="0,20,0"
            FontSize="55"
            Foreground="{Binding TextColor,Converter={StaticResource ColorConverter},ConverterParameter=DefaultTextForegroundThemeBrush}"
            Style="{ThemeResource SubtitleTextBlockStyle}"
            Text="{Binding Title,Converter={StaticResource LowerConverter}}"
            Visibility="{Binding Text,RelativeSource={RelativeSource Mode=Self},Converter={StaticResource CollapseWhenEmpty}}" />
    </DataTemplate>
</Application.Resources>

在您的customTableView的UWP渲染器中,您可以手动设置listview.GroupStyle.FirstOrDefault().HeaderTemplate,如下所示。

class CustomTableViewRender : TableViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
    {
        base.OnElementChanged(e);

        if(Control != null)
        {
            var listview = Control as Windows.UI.Xaml.Controls.ListView;
            listview.GroupStyle.FirstOrDefault().HeaderTemplate = (Windows.UI.Xaml.DataTemplate)Windows.UI.Xaml.Application.Current.Resources["TableSectionOne"];
           
        }
    }   
}

不幸的是,TableSection不支持继承,因此我们无法为其扩展依赖项属性。

关于Title的大小写,您只需从Converter={StaticResource LowerConverter}中删除Text,然后将Title的{​​{1}}设置为带有任意大小写的字符串,并且不会转换为小写。所以最终看起来像这样:

TableView