检索自定义列表框项值

时间:2012-05-15 07:56:13

标签: vb.net windows-phone-7.1

这就是我将2个文本块添加到列表框中的方式

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)

但是,当我点击vb.net中的列表框时,如何检索所选项目值?

我试过这段代码

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

但它总是返回“lblScore_1”的最后一个输入

任何帮助

1 个答案:

答案 0 :(得分:0)

你必须:
- 选择所选项目;
- 将其投射到StackPanel;
- 访问stackpanel的孩子;
- 将子项转换为TextBlocks,然后根据需要读取它们的属性

使用数据绑定会使这更容易。