Can anyone help me figure out a way to retreive the content of a tab header in WPF?
All I want is the text in the header so I can assign it to some other variable, but there d开发者_如何学Pythonoesn't seem to be any way of getting at it.
I am very new to WPF.. but the last hour or so googling this problem has not returned anything helpful.
Thanks
XAML:
<TabControl x:Name="tabControl">
<TabItem>
<TabItem.Header>
<TextBlock>SomeText</TextBlock>
</TabItem.Header>
</TabItem>
</TabControl>
To access a Text:
((System.Windows.Controls.TextBlock)(((System.Windows.Controls.HeaderedContentControl)(this.tabControl.Items[0])).Header)).Text
To acces a Content Control:
(System.Windows.Controls.HeaderedContentControl)(this.tabControl.Items[0])).Header)
You know how casting and such works, right?
<TabControl>
<TabItem Name="_tabItem1" Header="MyHeader"/>
</TabControl>
//Header is an object and hence needs to be casted for retrieval as string
string headerText = (string)_tabItem1.Header;
MessageBox.Show(headerText);
TabItem.Header
can be anything, even complex controls so if you did not set this to a string yourself you cannot retrieve it as string like this either.
Use TabItem.Header
e.g. following code will set header
of the first tab to "New header":
(tabControl1.Items[0] as TabItem).Header="New header";
精彩评论