2017-08-09 151 views
0

我想獲取WPF TabItem中存在的圖形元素的內容。探索WPF TabItem

考慮爲窗口下面的代碼:

<TabItem x:Name="TheItem" Header="Form"> 
    <Grid x:Name="MainGrid"> 
     <TextBox x:Name="txtContent" Text="Hello I'm some content !" /> 
     <TextBox x:Name="txtOther" Text="Some other content" /> 
    </Grid> 
</TabItem> 

我看過了TabItem documentation on MSDN但未能找到任何有用的信息。

有什麼辦法從TabItem中獲取「txtContent」文本框中的內容嗎?

+0

您可以從後面'txtContent.Text'代碼得到這個或使用MVVM並綁定該內容到視圖模型.. –

回答

1

試試這個:

Grid grid = TheItem.Content as Grid; 
TextBox txtContent = grid.Children[0] as TextBox; 
string text = txtContent.Text; 

或者乾脆:

TextBox txtContent = MainGrid.Children[0] as TextBox; 
+0

第一個選項完美運作! – Loki