We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Display Group Key in column

I need show my group key in column in my SfListView.

Please see the image attachment.

Attachment: GroupColumn_c36f11f0.zip

1 Reply

GP Gnana Priya Namasivayam Syncfusion Team November 21, 2019 09:45 AM UTC

Hi Laerte, 
  
  
Thank you for using Syncfusion products. 
  
We have checked the reported query “Display group key values in a separate row” and You can achieve your requirement by adding groups keys in the separate layout like StackLayout or Grid in code behind and create key labels while loading listview as in the following so that Key’s will be generated only for the present groups. 
  
Xaml 
  
<ContentPage.Content> 
    <Grid > 
        <Grid.RowDefinitions> 
           <RowDefinition Height="*"/> 
        </Grid.RowDefinitions> 
            <syncfusion:SfListView  x:Name="listView" ItemSize="80" Grid.Row="0" 
                                    IsStickyGroupHeader="True" SelectionMode="None" 
                                   ItemsSource="{Binding contactsinfo}"> 
            </syncfusion:SfListView> 
            <Grid Grid.Row="0" x:Name="LabelForGrid" HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand"/> 
    </Grid> 
</ContentPage > 
  
  
Behavior.cs 
  
public class Behavior : Behavior<ContentPage> 
{ 
        private Grid grid; 
        private Label label; 
        private Label previousLabel; 
        public SfListView ListView { get; private set; } 
  
        protected override void OnAttachedTo(ContentPage bindable) 
        { 
            ListView = bindable.FindByName<SfListView>("listView"); 
            grid = bindable.FindByName<Grid>("LabelForGrid"); 
            ListView.DataSource.GroupDescriptors.Add(new GroupDescriptor() 
            { 
                PropertyName = "ContactName", 
                KeySelector = (object obj1) => 
                { 
                    var item = (obj1 as Contacts); 
                    return item.ContactName[0].ToString(); 
                } 
            }); 
            ListView.Loaded += ListView_Loaded;  
            base.OnAttachedTo(bindable); 
        } 
  
        private void ListView_Loaded(object sender, ListViewLoadedEventArgs e) 
        { 
            var groupcount = this.ListView.DataSource.Groups.Count; 
            for (int i = 0; i < groupcount; i++) 
            { 
                label = new Label();  //New labels created based on the group header key value 
                GroupResult group = ListView.DataSource.Groups[i]; 
                label.Text = group.Key.ToString(); 
                grid.Children.Add(label, 0, i); 
                var labelTapped = new TapGestureRecognizer() { Command = new Command<object>(OnTapped), CommandParameter = label }; 
                label.GestureRecognizers.Add(labelTapped); 
            } 
        } 
        private void OnTapped(object obj) 
        { 
            if (previousLabel != null) 
            { 
                previousLabel.TextColor = Color.DimGray; 
            } 
            var label = obj as Label; 
            var text = label.Text; 
            label.TextColor = Color.Red; 
            for (int i = 0; i < ListView.DataSource.Groups.Count; i++) 
            { 
                var group = ListView.DataSource.Groups[i]; 
  
                if (group.Key == null) 
                    App.Current.MainPage.DisplayAlert("Message", "Group not available", "ok"); 
                if ((group.Key != null && group.Key.Equals(text))) 
                { 
                    ListView.LayoutManager.ScrollToRowIndex(ListView.DataSource.DisplayItems.IndexOf(group), true); 
                } 
            } 
            previousLabel = label; 
        } 
} 
  
  
We have attached the model sample for your reference, please find the sample from the following link. 
  
  
Additionally, we would like to inform that you can also use Paging control to achieve your requirement. We had already published documentation about the Listview and Paging. Kindly refer to our documentation from the following link. 
  
  
We hope this helps. Please let us know if you need any further assistance. 
 
Regards, 
Gnana Priya N 


Loader.
Live Chat Icon For mobile
Up arrow icon