Customization in .NET MAUI DataPager

18 Nov 201824 minutes to read

The DataPager allows you to customize its appearance and layout to match your application’s design requirements.

Numeric button shapes

The SfDataPager allows you to change the shape of the buttons using the SfDataPager.ButtonShape property.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           ButtonShape="Rectangle"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.ButtonShape = DataPagerButtonShape.Rectangle;
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Numeric button shape .NET MAUI DataPager.

Generating numeric buttons

The SfDataPager allows you to choose the generation mode of numeric buttons using the SfDataPager.NumericButtonsGenerateMode property. The numeric buttons can be generated either automatically in view or by specifying the count directly in the SfDataPager.NumericButtonCount property.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           NumericButtonsGenerateMode="Auto"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.NumericButtonsGenerateMode = DataPagerNumericButtonsGenerateMode.Auto;
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Note: The size of the SfDataPager is automatically adjusted based on the available screen size if the view cannot accommodate the numeric buttons specified in the NumericButtonCount property.

Customizing button size and font size of pager buttons

The SfDataPager button is loaded with a default width and height of 40. The default button font size of SfDataPager is 14. You can customize the button size and font size by setting the desired values for the SfDataPager.ButtonSize and SfDataPager.ButtonFontSize properties, respectively.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           ButtonSize="60"
                           ButtonFontSize="21"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.ButtonSize = 60;
dataPager.ButtonFontSize = 21;
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Button size and font size of .NET MAUI DataPager.

Customizing button spacing

The SfDataPager buttons are loaded with a default spacing of 8. You can customize the button spacing by setting the desired values for the SfDataPager.ButtonSpacing.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           ButtonSpacing="15"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.ButtonSpacing = 15;
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Button size and font size of .NET MAUI DataPager.

Display mode

The visibility of the numeric and navigation buttons can be personalized by using the SfDataPager.DisplayMode property. The default value is FirstLastPreviousNextNumeric, which displays all navigation and numeric buttons.

Property Value Description

None

Displays no page buttons.

First

Displays only the first page button.

Last

Displays only the last page button.

Previous

Displays only the previous page button.

Next

Displays only the next page button.

Numeric

Displays only the numeric page buttons.

FirstLast

Displays the first and last page buttons.

PreviousNext

Displays the previous and next page buttons.

FirstLastNumeric

Displays the first, last and numeric page buttons.

PreviousNextNumeric

Displays the previous, next and numeric page buttons.

FirstLastPreviousNext

Displays the first, last, previous and next page buttons.

FirstLastPreviousNextNumeric

Displays the first, last, numeric, previous and next page buttons.
<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           DisplayMode="FirstLastNumeric"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.DisplayMode = DataPagerDisplayMode.FirstLastNumeric;
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Auto-ellipsis mode

The AutoEllipsisMode property controls whether ellipsis buttons appear for navigating large ranges of page numbers, whereas the DisplayMode property controls which button types (First, Last, Previous, Next, Numeric) are visible. The SfDataPager offers support for displaying an ellipsis button at the beginning and end of the numeric buttons when the scroll view contains additional numeric buttons before or after the currently selected numeric button. It can be customized by using the SfDataPager.AutoEllipsisMode property.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           AutoEllipsisMode="After"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.AutoEllipsisMode = DataPagerEllipsisMode.After;
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Auto-ellipsis mode .NET MAUI DataPager.

Customize the auto-ellipsis text

The auto-ellipsis text can be customized by using the SfDataPager.AutoEllipsisText property. The default value of AutoEllipsisText is set to ….

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           AutoEllipsisMode="After"
                           AutoEllipsisText="***"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.AutoEllipsisMode = DataPagerEllipsisMode.After;
dataPager.AutoEllipsisText = "***";
dataPager.Source = viewModel.Orders;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Orientation

By default, SfDataPager displays buttons in the horizontal direction. However, the SfDataPager.Orientation property allows users to customize the data pager to display the buttons vertically or horizontally according to their preferences.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Border Grid.Column="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15" 
                           Orientation="Vertical"
                           Source="{Binding Orders}">
        </pager:SfDataPager>
    </Border>
    <syncfusion:SfDataGrid x:Name="dataGrid"
                           Grid.Column="0"
                           ItemsSource="{Binding Source={x:Reference dataPager}, Path=PagedSource}">
    </syncfusion:SfDataGrid>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.Orientation = DataPagerScrollOrientation.Vertical;
dataPager.Source = viewModel.Orders;

SfDataGrid dataGrid = new SfDataGrid();
dataGrid.ItemsSource = dataPager.PagedSource;

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
grid.Children.Add(dataGrid);
grid.Children.Add(border);
grid.SetColumn(dataGrid, 0);
grid.SetColumn(border, 1);
this.Content = grid;

The SfDataPager allows you to replace the default icons of the first, previous, next, and last page buttons with your own content using the following properties. Each property accepts a DataTemplate whose root element is a View.

The default value of each property is null, which renders the built-in icon. The button itself, including its tap handling, enabled and disabled states, and background, is still provided by the pager. Only the content inside the button is replaced.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15"
                           Source="{Binding Orders}">
            <pager:SfDataPager.FirstPageButtonTemplate>
                <DataTemplate>
                    <Label Text="First" HorizontalOptions="Center" VerticalOptions="Center" />
                </DataTemplate>
            </pager:SfDataPager.FirstPageButtonTemplate>
            <pager:SfDataPager.PreviousPageButtonTemplate>
                <DataTemplate>
                    <Label Text="&lt;" HorizontalOptions="Center" VerticalOptions="Center" />
                </DataTemplate>
            </pager:SfDataPager.PreviousPageButtonTemplate>
            <pager:SfDataPager.NextPageButtonTemplate>
                <DataTemplate>
                    <Label Text="&gt;" HorizontalOptions="Center" VerticalOptions="Center" />
                </DataTemplate>
            </pager:SfDataPager.NextPageButtonTemplate>
            <pager:SfDataPager.LastPageButtonTemplate>
                <DataTemplate>
                    <Label Text="Last" HorizontalOptions="Center" VerticalOptions="Center" />
                </DataTemplate>
            </pager:SfDataPager.LastPageButtonTemplate>
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.Source = viewModel.Orders;

dataPager.FirstPageButtonTemplate = new DataTemplate(() => new Label
{
    Text = "First",
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
});
dataPager.PreviousPageButtonTemplate = new DataTemplate(() => new Label
{
    Text = "<",
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
});
dataPager.NextPageButtonTemplate = new DataTemplate(() => new Label
{
    Text = ">",
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
});
dataPager.LastPageButtonTemplate = new DataTemplate(() => new Label
{
    Text = "Last",
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
});

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Navigation button templates .NET MAUI DataPager.

The icon color settings of DataPagerStyle apply only to the built-in icons, not to template content.

Numeric button template

The NumericButtonTemplate property customizes the content of every numeric page button. The default value is null, which renders the built-in page number text.

The BindingContext of the template content is a BindingContextHelper object. Its PageNumber property holds the one-based page number shown by that button, so you can bind to it with {Binding PageNumber}. When the numeric buttons are recycled during scrolling, PageNumber is updated and the binding refreshes automatically.

<ContentPage.BindingContext>
    <local:OrderInfoViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                           PageSize="15"
                           Source="{Binding Orders}">
            <pager:SfDataPager.NumericButtonTemplate>
                <DataTemplate>
                    <Label Text="{Binding PageNumber, StringFormat='P{0}'}"
                           HorizontalOptions="Center"
                           VerticalOptions="Center"/>
                </DataTemplate>
            </pager:SfDataPager.NumericButtonTemplate>
        </pager:SfDataPager>
    </Border>
</Grid>
SfDataPager dataPager = new SfDataPager();
OrderInfoViewModel viewModel = new OrderInfoViewModel();
dataPager.PageSize = 15;
dataPager.Source = viewModel.Orders;

dataPager.NumericButtonTemplate = new DataTemplate(() =>
{
    var label = new Label
    {
        HorizontalOptions = LayoutOptions.Center,
        VerticalOptions = LayoutOptions.Center
    };
    label.SetBinding(Label.TextProperty, new Binding("PageNumber", stringFormat: "P{0}"));
    return label;
});

Border border = new Border();
border.Padding = new Thickness(5);
border.Content = dataPager;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.Children.Add(border);
grid.SetRow(border, 1);
this.Content = grid;

Numeric button template in .NET MAUI DataPager.

Choosing a template based on the page number

NumericButtonTemplate also accepts a DataTemplateSelector. The selector’s item argument is the same BindingContextHelper object, so you can choose a template by page number. The template is re-evaluated when buttons are recycled.

<ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="evenTemplate">
            <Label
                Text="Even",
                TextColor="Green"
                HorizontalTextAlignment="Center"
                VerticalTextAlignment="Center" />
        </DataTemplate>
        <DataTemplate x:Key="oddTemplate">
            <Label
                Text="Odd"
                TextColor="Red
                HorizontalTextAlignment="Center"
                VerticalTextAlignment="Center" />
        </DataTemplate>
        <local:NumericButtonTemplateSelector x:Key="NumericButtonSelector"
                                    EvenPageTemplate="{StaticResource evenTemplate}"
                                    OddPageTemplate="{StaticResource oddTemplate}" />
    </ResourceDictionary>
</ContentPage.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="1" Padding="5">
        <pager:SfDataPager x:Name="dataPager"
                   PageSize="10"
                   Source="{Binding Orders}" 
                   NumericButtonTemplate="{StaticResource NumericButtonSelector}">
        </pager:SfDataPager>
    </Border>
</Grid>
public class PageTemplateSelector : DataTemplateSelector
{
    public DataTemplate? EvenPageTemplate { get; set; }
    public DataTemplate? OddPageTemplate { get; set; }

    protected override DataTemplate? OnSelectTemplate(object item, BindableObject container)
    {
        var context = (BindingContextHelper)item;
        return context.PageNumber % 2 == 0 ? EvenPageTemplate : OddPageTemplate;
    }
}

dataPager.NumericButtonTemplate = new PageTemplateSelector
{
    EvenPageTemplate = new DataTemplate(() => new Label { Text = "Even", TextColor = Colors.Green, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center }),
    OddPageTemplate = new DataTemplate(() => new Label { Text = "Odd", TextColor = Colors.Red, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center })
};

If the template (or the template returned by the selector) does not produce a View, the pager falls back to the built-in page number text. Ellipsis buttons are not affected by NumericButtonTemplate; their text is controlled by AutoEllipsisText. The NumericButtonTextColor and NumericButtonSelectionTextColor style settings apply only to the built-in text, so style the template content yourself if you need to distinguish the current page.