xamarin-forms-docs/knowledge-base/listview-bind-layout-spanco...

1.7 KiB

title description type page_title slug tags res_type
How to bind ListViewGridLayout SpanCount property of RadListView? How to change the SpanCount with a binding at runtime? how-to RadListView GridLayout SpanCount Binding listview-bind-layout-spancount listview, spancount, binding kb

Environment

Product Version R2 2021
Product ListView for Xamarin

Description

Let's have the following RadListView with ListViewGridLayout configured as follow:

<telerikDataControls:RadListView x:Name="listView"
                                ItemsSource="{Binding Source}">
    <telerikDataControls:RadListView.LayoutDefinition>
        <telerikListView:ListViewGridLayout SpanCount="2"
                                            ItemLength="120"/>
    </telerikDataControls:RadListView.LayoutDefinition>
</telerikDataControls:RadListView>

The requirement is to update the SpanCount property through a binding.

Solution

The tricky part is the ListViewGridLayout does not receive the binding context of the ListView directly. The binding context should be explicitly applied like this:

<telerikDataControls:RadListView x:Name="listView"
                                ItemsSource="{Binding Source}">
    <telerikDataControls:RadListView.LayoutDefinition>
        <telerikListView:ListViewGridLayout SpanCount="{Binding BindingContext.SpanCountValue, Source={x:Reference listView}}"
                                            ItemLength="120"/>
    </telerikDataControls:RadListView.LayoutDefinition>
</telerikDataControls:RadListView>