Articles in this section
Category / Section

How to access element which is loaded in the DataTemplate of ItemTemplate?

1 min read

The SfListView allows to access the elements which is loaded inside DataTemplate of the ItemTemplate by extending the ListViewItem. In the extended ListViewItem class, you can get the element by overriding the OnChildAdded method.

 

Refer the below code snippet which illustrates how to extend the ItemGenerator.

 

C#

public class ItemGeneratorExt : ItemGenerator 
{ 
   public SfListView listView; 
 
   public ItemGeneratorExt(SfListView listView) : base(listView) 
     { 
         this.listView = listView; 
     } 
 
   protected override ListViewItem OnCreateListViewItem(int itemIndex, ItemType type, object data = null) 
     { 
        if (type == ItemType.Record) 
            return new ListViewItemExt(this.listView); 
        return base.OnCreateListViewItem(itemIndex, type, data); 
     } 
} 

 

 
Initialize and assign ItemGenerator extension to SfListView.

 

C#

public partial class MainPage : ContentPage 
{ 
  public MainPage() 
    { 
       InitializeComponent(); 
       this.listView.ItemGenerator = new ItemGeneratorExt(this.listView); 
    } 
} 

 

The below code snippet which illustrates how to extend the ListViewItem and how to override the OnChildAdded method to get the element which is loaded in the DataTemplate. 

 

C#

public class ListViewItemExt : ListViewItem 
{ 
  private SfListView listView; 
 
  public ListViewItemExt(SfListView listView) 
    { 
         this.listView = listView; 
    } 
  protected override void OnChildAdded(Element child) 
    { 
          var label = child;   //You can access label and perform your code here
          base.OnChildAdded(child); 
    } 
} 

 

Click to here download source code.

 

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