2012-06-28 54 views
0

在WPF中,我動態地創建了幾個TextBoxes,然後,我去查找我創建它們的Canvas的所有子對象。在我搜索時,我可以獲得文本框的名稱,但是如何更改文本框中的文本?如何更新動態創建的文本框中的文本wpf

我曾嘗試:

// oText is the visual object I found when searching for the textbox 
oText.Text = "Software" // doesnt work. 
oText.SetValue(control.Text) // doesnt work, because there is no .text property 

甚至通過我可以調試它,將鼠標懸停在oText對象,向下滾動並找到Text屬性被設置爲"Software",但我不喜歡閱讀我可以用

oText.GetValue(control.width) 

我們如何讀取這個動態創建的文本框的WPF文本值?

下面是代碼:

我創建XAML畫布:

<Canvas x:Name="Can1" Height="700" Width="874"> 

     </Canvas> 

然後,我讓文本框,並把它們在畫布上...

For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1 
     ' Retrieve child visual at specified index value. 
     Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual) 
     ' Return the offset vector for the TextBlock object. 
     Dim vector As Vector = VisualTreeHelper.GetOffset(childVisual) 
     ' Convert the vector to a point value. 
     Dim currentPoint As New Point(VisualOffset.X, VisualOffset.Y) 
     x = Canvas.GetLeft(childVisual) 
     y = Canvas.GetTop(childVisual) 

     A = childVisual.GetValue(Control.ActualHeightProperty) 
     B = childVisual.GetValue(Control.ActualWidthProperty)  

     Dim myTextbox As New TextBox 
     Dim c As Int16 
     myTextbox.Width = B 
     myTextbox.Text = "Software" 
     myTextbox.Name = "TextB" & i.ToString 
     Can1.Children.Add(myTextbox) 
     Canvas.SetTop(myTextbox, y + A) 
     Canvas.SetLeft(myTextbox, x) 
     Canvas.SetZIndex(myTextbox, 0) 
next i 

然後,我使用主窗口上的一個按鈕來調用GetData ...

Private Sub GetData(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
    Dim iCount As Int16 
    Dim oText As Visual 
    Dim sTemp As String 
    Dim cs = My.Settings.ConnectionString 
    Dim oConn = New SqlConnection(cs) 
    Dim cmd As New SqlCommand() 
    Text1.text = "" 
    cmd.Connection = oConn 
    Try 
     oConn.Open() 
     cmd.CommandText = "select top 5 finumber from fiheading " 
     Dim myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) 
     While myReader.Read() 
      iCount += 1 
      oText = FindChild(Can1, "TextB" & iCount.ToString) 
      'sTemp = oText.GetValue(Control.NameProperty) 
      'oText.text = (myReader.GetString(0)) 
      'oText.SetValue(Control.text) 

     End While 
     myReader.Close() 
    Catch ex As Exception 
     MsgBox(ex.Message) 

    End Try 


    Try 
     oConn.Close() 
    Catch ex As Exception 
     MsgBox(ex.Message) 

    End Try 


End Sub 
+0

是'的oText'鍵入'TextBox'?或者它是其他通用的,比如'FrameworkElement'或'object'? – Rachel

+0

好的,我把一些代碼放在上面的窗口中。我創建了文本框,然後在按下按鈕時嘗試找到它們。我從SQL數據庫獲取數據,正如你所看到的,我有幾條註釋掉的線路,我嘗試了不同的東西。所以這個對象是我在畫布上看到的一個孩子的視覺。我可以在調試中將鼠標懸停在它上面,然後單擊「+」並展開屬性以查看是否有文本屬性=「軟件」,但我不知道如何讀取它或寫入它。 – SDanks

回答

1

oText被定義爲Visual,它不具有Text財產

將其更改爲TextBox及演員FindChild結果爲TextBox,它應該正常工作

Dim oText As TextBox 
... 

oText = CType(FindChild(Can1, "TextB" & iCount.ToString), TextBox) 
+0

是的,就是這樣。謝謝瑞秋。我想我以爲它是在調試模式下顯示文本,它是返回正確的信息。我會更注意它返回的對象的類型。再次感謝。 – SDanks