2012-05-15 36 views
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」 最後輸入

任何幫助

回答

0

你必須:
- 以所選項目;
- 將其轉換爲StackPanel;
- 訪問堆疊面板的孩子;
- 將子項投射到TextBlocks,然後根據需要讀取它們的屬性

使用數據綁定會使這更容易。

+0

您好,我嘗試這個代碼 昏暗LBI作爲一個ListBoxItem = ListScore.ItemContainerGenerator.ContainerFromIndex(ListScore.SelectedIndex) 昏暗STK作爲的StackPanel = lbi.Content 昏暗RS_1作爲TextBlock的= stk.FindName( 「lblScore_1」) Score_1.Text = rs_1.Text 但它總是返回「lblScore_1」的最後一個輸入 任何幫助 –