I created a Hub like this:

<Hub x:Name="Hub" x:Uid="Hub" Margin="0,76,0,0"> <HubSection x:Uid="HubSection1" Header="Карта" FontSize="20"> <DataTemplate> <my:MapControl /> </DataTemplate> </HubSection> <HubSection x:Name="hb2" x:Uid="HubSection2" Header="Местоположения"> <DataTemplate> <Grid x:Name="gridm"> <ComboBox > <ComboBoxItem Content="Последняя" IsSelected="True"/> <ComboBoxItem Content="За последние сутки"/> <ComboBoxItem Content="За неделю"/> </ComboBox> <ListView x:Name="list" Foreground="White" Margin="5,58,5,5" ItemClick="list_ItemClick" SelectionChanged="list_SelectionChanged" Header="123456123" RequestedTheme="Light" SizeChanged="list_SizeChanged"> </ListView> </Grid> </DataTemplate> </HubSection> </Hub> 

So, I can’t get through to ListView. C # constantly writes that list is not available in this context ... How can I get to the list in my case?

Example

  • Which file do you want to reach? In your case should be * .xaml.cs - Gardes
  • @ S.Kos Exactly! A piece of markup code is provided from the Page1.xaml file. A screenshot of Page1.xaml.cs. - Geri4
  • Try changing x:Name="list" to Name="list - Gardes
  • @ S.Kos did not help, he doesn’t see further HubSection name ... that is, gridm doesn’t see, etc. ( - Geri4
  • And why are you not using MVVM, sorry for the question? - VladD

1 answer 1

 private ListView getListFromElement(Hub hub) { for (int i = 0; i < hub.Children.Count; i++) { if (hub.Children[i] is ListView) { ListView list = (ListView)hub.Children[i]; return list; } } return null; } 

Call this:

  ListView list = (ListView)getListFromElement(Hub); 
  • Strange hub.Children writes that the method is invalid in this context ... - Geri4
  • I in the documentation did not find the Children method at the hub at all. - Geri4
  • I think that since the DataTemplate is set by HubSelection, it is impossible to get access from the code-behind - Gardes
  • Thanks for the help, sorry, I will look for options further ... I don’t care about ListView, I was able to fill it up differently, but I can’t get to MapControl, that's bad ( - Geri4
  • figured out =) You can assign an event to a loaded control, for example, create an instance, the desired control, when an event to occur, an object is written to the instance that triggered. For example, private MapControl myMapControl; private void myMapControl_Loaded (object sender, RoutedEventArgs e) {myMapControl = (sender as MapControl);} - Geri4