2017-08-09 147 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