This is how I add the 2 textblock to my listbox as an item
Dim StkP As New StackPanel
StkP.Name = "Stack"
Dim txtLine_1 As New TextBlock
txtLine_1.FontSize = 24
txtLine_1.Name = "txtLine_1"
txtLine_1.Text = "Units " + txtUnits_1.Text + ", Cost " + txtCost_1.Text
Dim txtLine_2 As New TextBlock
txtLine_2.FontSize = 24
txtLine_2.Name = "txtLine_2"
txtLine_2.Text = txtResult_1.Text
StkP.Children.Add(txtLine_1)
StkP.Children.Add(txtLine_2)
ListCompare.Items.Add(StkP)
But how can I retrieve the selected items values when I tap on listbox in vb.net?
I tried this code
Dim lbi As ListBoxItem = ListScore.ItemContainerGenerator.ContainerFromIndex(ListScore.SelectedIndex)
Dim stk As StackPanel = lbi.Content
Dim rs_1 As TextBlock = stk.FindName("lblScore_1")
Score_1.Text = rs_1.Text
But it is always returning the last input of “lblScore_1”
Any help
You’ll have to:
– take the selected item;
– cast it to a StackPanel;
– access the children of the stackpanel;
– cast the child items to TextBlocks and then read their properties as necessary
Using databinding would make this much easier.