Search code examples
xamarinwin-universal-appuwpwindows-10-universalxamarin.windows

TextCellRenderer in Xamarin.UWP


How could we set TextCell height with padding/margin in Windows Universal Project using Xamarin.Forms?

I tried following Custom renderer in Native project:

class CustomTextCellRenderer : TextCellRenderer
{
    public override Windows.UI.Xaml.DataTemplate GetTemplate(Cell cell)
    {
        var d = base.GetTemplate(cell);
        //Set something here???
        return d;
    }
}

but couldn't find any property to be set.

There is function to set dependency property of DataTemplate but I coudn't figure it out, what will be the dependency property name for height to set?

d.SetValue(???DependencyProperty???, value);

Solution

  • Since the TextCell is a built in cell, it's optimized and designed to be used as is. However you can create a custom DataTemplate based on the TextCell template and return that instead.

    You can find the original template in the Xamarin.Forms source. Then in the UWP platform project's App.xaml, define your new DataTemplate inside the ResourceDictionary with a different key:

    <DataTemplate x:Key="MyTextCell">
        <StackPanel Background="Aqua">
            <TextBlock
                Padding="0 10 0 10"
                Margin="5"
                Text="{Binding Text}"
                Style="{ThemeResource BaseTextBlockStyle}" />
    
            <TextBlock
                Text="{Binding Detail}"
                Style="{ThemeResource BodyTextBlockStyle}"
                x:Name="detail"/>
        </StackPanel>
    </DataTemplate>
    

    and access it inside the custom renderer with:

    return App.Current.Resources["MyTextCell"] as Windows.UI.Xaml.DataTemplate;