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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to access a named ListView inside a XAML DataTemplate in Xamarin.Forms (SfListView)?

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: March 25, 2020 |
Last Revised Date: April 8, 2020

You can access the named SfListView defined inside DataTemplate of PopupLayout by using Behavior.

XAML

In PopupLayout’s PopupView, behaviour added to parent of ListView.

<sfPopup:SfPopupLayout x:Name="popupLayout">
    <sfPopup:SfPopupLayout.PopupView>
        <sfPopup:PopupView >
            <sfPopup:PopupView.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.Behaviors>
                            <local:GridBehavior/>
                        </Grid.Behaviors>                            
                        <Button Text="Find ListView" x:Name="listviewButton" />
                        <sfListView:SfListView   x:Name="listView"  
                                                    ItemsSource="{Binding Items}" >
                            <sfListView:SfListView.ItemTemplate>
                                <DataTemplate>
                                     …
                                </DataTemplate>
                            </sfListView:SfListView.ItemTemplate>
                        </sfListView:SfListView>
                    </Grid>
                </DataTemplate>
            </sfPopup:PopupView.ContentTemplate>
        </sfPopup:PopupView>
    </sfPopup:SfPopupLayout.PopupView>
    <sfPopup:SfPopupLayout.Content>
        <Grid>
            <Button x:Name="ShowPopup" Text="Bring Popup"/>
        </Grid>
    </sfPopup:SfPopupLayout.Content>
</sfPopup:SfPopupLayout>

C#

In ChildAdded event you will get the instance of ListView.

public class GridBehavior : Behavior<Grid>
{
    Grid grid;
    SfListView listView;
    protected override void OnAttachedTo(BindableObject bindable)
    {
        grid = bindable as Grid;
        grid.ChildAdded += Grid_ChildAdded;
    }
    //Method 1 : Get SfListView reference using Grid.ChildAdded Event
    private void Grid_ChildAdded(object sender, ElementEventArgs e)
    {
        if (e.Element is SfListView)
        {
            listView = e.Element as SfListView;
            listView.RefreshView();
        }
    }
    protected override void OnDetachingFrom(BindableObject bindable)
    {
        grid.ChildAdded -= Grid_ChildAdded;
        listView = null;
        grid = null;
        base.OnDetachingFrom(bindable);
    }
}

C#

You can also get the ListView using FindByName method from Parent element.

public class GridBehavior : Behavior<Grid>
{
    Grid grid;
    SfListView listView;
    Button button;
    protected override void OnAttachedTo(BindableObject bindable)
    {
        grid = bindable as Grid;
        grid.ChildAdded += Grid_ChildAdded;
    }
    private void Grid_ChildAdded(object sender, ElementEventArgs e)
    {
        if (e.Element is Button)
        {
            button = e.Element as Button;
            button.Clicked += Button_Clicked;
        }
    }
    //Method 2 : Get SfListView reference using FindByName
    private void Button_Clicked(object sender, EventArgs e)
    {
        listView = grid.FindByName<SfListView>("listView");
        App.Current.MainPage.DisplayAlert("Information", "ListView instance obtained", "Ok");
        listView.ItemTapped += ListView_ItemTapped;
    }
 
    private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
    {
        App.Current.MainPage.DisplayAlert("Information", "ListView ItemTapped", "Ok");
    }
 
    protected override void OnDetachingFrom(BindableObject bindable)
    {
        button.Clicked -= Button_Clicked;
        grid.ChildAdded -= Grid_ChildAdded;
        listView.ItemTapped -= ListView_ItemTapped;
        listView = null;
        button = null;
        grid = null;
        base.OnDetachingFrom(bindable);
    }
}

View sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile