Access a TextBlock from a specific ListViewItem from a list in Windows Phone 8.1 XAML programmatically

I am a new developer on Windows Phone 8.1, I'm trying to reach a specific ListView element from the ListView collection and be able to color it or the TextBock color inside it, but I can’t get to the item or reach any of the elements inside the ListView, please see my code below:

protected async override void OnNavigatedTo(NavigationEventArgs e) { SQLiteRT db1 = new SQLiteRT(); var db_connection = await db1.Connection("MyDB.sqlite"); List<MyTBL> t_list = db1.GetTable("SELECT * FROM MyTBL LIMIT 4 ORDER BY RANDOM() ;"); db_connection.Close(); LV_Options.ItemsSource = t_list; } // my List View called LV_Options private void LV_Options_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListView lv1 = sender as ListView; if (lv1 == null) return; MyTBL wrd = lv1.SelectedItem as MyTBL; if (wrd == null) return; TextBlock tb = lv1.FindName("TB_AMean1") as TextBlock; tb.FontSize = 17; // here I got debug error (it not worked !!!!!!!) var item = LV_Options.Items.ElementAt(3); // this seems not work also !!!! item.BackColor = Color.LightSteelBlue; } 

As you can see above, I tried to reach a specific LV_Options.Items.ElementAt (3) element, but it does not work! I also tried to get to the TextBlock from the selected View List element, but also does not work!

(Updated) XAML Code:

 <!-- Title Panel --> <StackPanel Grid.Row="0" Margin="19,0,0,0"> <TextBlock Name="TB_Rslt" Text="Here result of your answer" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0"/> <TextBlock Text="page title" Margin="0,-6.5,0,26.5" Style="{ThemeResource HeaderTextBlockStyle}" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"/> </StackPanel> <!--TODO: Content should be placed within the following grid--> <Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,10,19,15"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Name="TB_Question" Text="Choose Answer " Margin="0,0,25,0" HorizontalAlignment="Right" FontWeight="Bold" FontSize="22" FontFamily="Verdana" RenderTransformOrigin="0.5,0.5" /> <TextBlock Name="TB_EnWord" Text="" Margin="90,0,15,0" HorizontalAlignment="Left" FontWeight="Bold" FontSize="22" FontFamily="Verdana" RenderTransformOrigin="0.5,0.5" TextAlignment="Right" /> <StackPanel Grid.Row="1" Margin="5,22,0,0"> <ListView Name="LV_Options" SelectionChanged="LV_Options_SelectionChanged"> <ListView.ItemTemplate> <DataTemplate> <Grid Margin="6"> <StackPanel VerticalAlignment="Top" Margin="10,0,0,0"> <TextBlock Name="TB_AMean1" Text="{Binding AMean1}" TextWrapping="Wrap"/> </StackPanel> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackPanel> <Button Name="Btn_Answer" Content="Ansewr" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Bottom" Click="Btn_Answer_Click"/> 

My application is a quiz application that offers 4 options / options as answers to each question, and when the user selects the correct answer, I want to highlight the true answer (true choice), turn its background green, and if the user selects the wrong answer / parameter. I want to make the background of this answer (specific list item) red.

Any help please?

+6
source share
2 answers

You cannot access the item inside the data template. Instead, use the binding to the view model to set the color and other properties associated with the view. First create a wrapper view model for your data class:

 public class MyTBLViewModel : INotifyPropertyChanged { public MyTBL Entity { get { return _entity; } } private readonly MyTBL _entity; public Brush Highlight { get { return _brush; } set { _brush = value; RaisePropertyChanged("Highlight"); } } private Brush _highlight; public double ItemFontSize { get { return _itemFontSize; } set { _itemFontSize = value; RaisePropertyChanged("ItemFontSize"); } } private Brush _itemFontSize; public MyTBLViewModel(MyTBL entity) { _entity = entity; _highlight = new SolidColorBrush(Colors.Transparent); _itemFontSize = 12; } public event PropertyChangedEventArgs PropertyChanged; protected void RaisePropertyChanged(string propName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propName)); } } 

Use this as your ItemsSource :

 List<MyTBLViewModel> t_list = db1.GetTable("SELECT * FROM MyTBL LIMIT 4 ORDER BY RANDOM() ;") .AsEnumerable().Select(entity => new MyTBLViewModel(entity)).ToList(); 

Now, in your opinion, bind the view elements to "Highlight" and "ItemFontSize" and to any other properties that you like:

 <ListView.ItemTemplate> <DataTemplate> <Grid Margin="6" Background="{Binding Highlight}"> <StackPanel VerticalAlignment="Top" Margin="10,0,0,0"> <TextBlock Name="TB_AMean1" Text="{Binding Entity.AMean1}" TextWrapping="Wrap" FontSize="{Binding ItemFontSize}"/> </StackPanel> </Grid> </DataTemplate> </ListView.ItemTemplate> 

Finally, you can get the data item from SelectionChangedEventArgs - use it to update the properties associated with the view:

 private void LV_Options_SelectionChanged(object sender, SelectionChangedEventArgs e) { foreach (var item in e.AddedItems.OfType<MyTBLViewModel>()) { item.Highlight = new SolidColorBrush(Color.LightSteelBlue); item.ItemFontSize = 17; } foreach (var item in e.RemovedItems.OfType<MyTBLViewModel>()) { item.Highlight = new SolidColorBrush(Colors.Transparent); item.ItemFontSize = 12; } } 
+3
source
 var item = LV_Options.Items.ElementAt(3); 

This line is incorrect. It will not return you a TextBlock . I do not know what .BackColor is and it should not compile. The Items property in ListView will return you a list of ListViewItems . If you want to access the internal element from a ListViewItem , you need to access the ContentTemplateRoot property.

Do not use var ever. It suggests that you know the type, whereas if you explicitly entered an ad, you will realize that you are doing it wrong.

  MyTBL wrd = lv1.SelectedItem as MyTBL; if (wrd == null) return; TextBlock tb = lv1.FindName("TB_AMean1") as TextBlock; 

What is MyTBL ? FindName is only available for the DependencyObjects framework, so I assume this is a user control? You have to provide a lot more code to show us what you are doing and what you are setting the ListView ItemsSource and ItemTemplate , and what these errors are, and how you have 2 to crack debugging errors right away and what a message error.

Understanding runtime error messages is a huge part of a good developer.

+2
source

Source: https://habr.com/ru/post/974253/


All Articles