Articles in this section
Category / Section

How to enable compiled binding for Xamarin.Forms ListView (SfListView)?

5 mins read

The Xamarin.Forms ListView has introduced compiled bindings to reduce CPU cycles and boost performance when using data binding.

You can enable compiled binding by using the special x:DataType attribute on any VisualElement in Xamarin.Forms SfListView.

You can also refer to the following document regarding the compiled bindings in Xamarin.Forms,

https://devblogs.microsoft.com/xamarin/compiled-bindings-xamarin-forms/

XAML

Define the namespace of the ViewModel and set BindingContext for the ContentPage.

<ContentPage xmlns:viewModel="clr-namespace:ListViewXamarin.ViewModel"
             x:DataType="viewModel:ContactsViewModel">
    <ContentPage.BindingContext>
        <viewModel:ContactsViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
            </syncfusion:SfListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

When you are using template control like SfListView. you need to set up complied binding for the DataTemplate by using x:DataTye.

<ContentPage xmlns:model="clr-namespace:ListViewXamarin.Model">
    <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate x:DataType="model:Contacts">
                <Grid x:Name="grid">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/>
                    <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center">
                        <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/>
                        <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/>
                    </Grid>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>
</ContentPage>

For SfListView.GroupHeaderTemplate, BindingContext of GroupHeaderTemplate will be the GroupResult that can be accessed from Syncfusion.DataSource.Extensions of Syncfusion.DataSource.

<ContentPage xmlns:result="clr-namespace:Syncfusion.DataSource.Extensions;assembly=Syncfusion.DataSource.Portable">
    <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
        <syncfusion:SfListView.GroupHeaderTemplate>
            <DataTemplate x:DataType="result:GroupResult">
                <Grid BackgroundColor="#E4E4E4">
                    <Label Text="{Binding Key}" FontSize="18" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Start" Margin="20,0,0,0" />
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.GroupHeaderTemplate>
    </syncfusion:SfListView>
</ContentPage>

Output

Demo image for using compiled binding for SfListView.itemTemplate and SfListView.GroupHeaderTemplate.

View sample in GitHub


Conclusion

I hope you enjoyed learning about how to enable compiled binding for Xamarin.Forms ListView.

You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied