Accessing a control dynamically inside a ListView?

I want to dynamically populate a TableView which is hosted inside a ViewCell of a ListView. The purpose is to render the control visible or invisible to allow the user to hierarchically browse the ListView. So when the user taps a ListView entry, I will populate the TableView dynamically and make it visible to render the subview

But how do I find the current topicTableView control inside the ViewCell of the ListView for the currently selected row?

    <ListView x:Name="categoryListView" ItemsSource="{Binding CategoryViewModels}"
                <StackLayout Orientation="Vertical">
                        Text="{Binding Name}"/>

This is the closest I found to anything that addresses this problem, but it discusses the solution with a ListBox and not a ListView. It traversed the tree of controls using the VisualTreeHelper, but since I am only interested in the currently selected row, it should be constant time to find the generated TreeView.

1 answer

  • answered 2018-03-11 13:33 Jacco Dieleman

    Browsing through the VisualTree is not something I would do. Especially because it differs a bit per platform. However this discussion may help you in walking the Visual Tree.

    If possible, I would choose to always populate the TableView the way it needs to be instead of populating when the user clicks the item.

    You can still dynamically make the TableView (In)Visible. That can be achieved by binding the IsVisible property to a IsTableViewVisible property on the CategoryViewModel. When the user taps the item, you can look up its ViewModel in the collection and flip the IsTableViewVisible property of it (be sure RaisePropertyChanged() is called for the property).

    Be sure to also read these two links because you end up with a ListView with uneven rows and runtime resizing rows.

    Good luck!