0

这就是我将 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”的最后一个输入

任何帮助

4

1 回答 1

0

你必须:
- 拿走所选择的物品;
- 将其转换为 StackPanel;
- 访问堆栈面板的孩子;
- 将子项转换为 TextBlocks,然后根据需要读取它们的属性

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

于 2012-05-15T08:26:15.197 回答