In XAML I can do like so:
<ListView x:Name="listView"
ItemsSource="{Binding ElementName=MainWindowName, Path=List}"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
/>
This will show a ListView without the scrollbar which is what I need. However, in my case, I need to add ListViews dynamically (1-10) as content in a PopUp. But the only way I have found to obtain the ScrollViewer in code is traversing the visual tree like in the examples here.
In code I have tried the below code:
var listView = new ListView();
listView.ItemsSource = list;
popup.Child = listView;
var scroller = listView.GetVisualChild<ScrollViewer>();
Here scroller is null. But if I add popup.IsOpen = true;
before getting the scroller, it works. I guess that is not surprising since the ListView has not been added to any tree? Of course, showing the popup before the user calls for it is not an option.
Can you suggest a way to make this work in code - or alternatively if there is a way to have dynamically ListViews in XAML.