Articles in this section
Category / Section

How to access a named ListView inside a XAML DataTemplate in .NET MAUI SfListView?

6 mins read

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

XAML

In SfPopup’s ContentTemplate, behaviour added to parent of ListView.

<popup:SfPopup x:Name="popupLayout">
    <popup:SfPopup.ContentTemplate>
        <DataTemplate>
            <Grid>
                <Grid.Behaviors>
                    <local:GridBehavior/>
                </Grid.Behaviors>
                
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                
                <Button Text="Find ListView" x:Name="listviewButton" BackgroundColor="LightGray"/>
                <sfListView:SfListView    x:Name="listView"  ItemSpacing="5" 
              ItemsSource="{Binding Items}" 
              SelectionMode="Single" Grid.Row="1">
                    <sfListView:SfListView.ItemTemplate>
                        <DataTemplate>
                            <Grid x:Name="grid" RowSpacing="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <Image Source="{Binding ContactImage}"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               HeightRequest="50"/>

                                <Label  Grid.Column="1"
                HorizontalTextAlignment="Center"
                HorizontalOptions="StartAndExpand"
                LineBreakMode="NoWrap"
                Text="{Binding ContactName}" 
                FontSize="Medium" />

                            </Grid>
                        </DataTemplate>
                    </sfListView:SfListView.ItemTemplate>
                </sfListView:SfListView>
            </Grid>
        </DataTemplate>
    </popup:SfPopup.ContentTemplate>
</popup:SfPopup>

C#

In ChildAdded event you will get the instance of ListView.

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;
    }
    
   //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;
    }
    //Method 1 : Get SfListView reference using Grid.ChildAdded Event
    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.Maui.ListView.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

Conclusion

I hope you enjoyed learning how to access a named ListView inside a XAML DataTemplate in .NET MAUI ListView.

You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET MAUI ListView example to understand how to create and manipulate data.

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 forums, Direct-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