Reputation: 1360
Let's say I have a form with two Tabs, each tab has 5 labels within it. I want to be able to type in a search box the label I want and it will be bring up that tab and highlight the label.
From what I have read I need to use the visual tree and search for the children, I have never done this before. I am wondering if there is an easier solution?
Note: I will always be searching labels text property
Upvotes: 0
Views: 32
Reputation: 61
If you name each grid which is the first child of a TabItem, you can then reference the children of the Grid. Then you could call and select the correct TabItem by using a Tag index of some sort to identify the parent TabItem's position in the TabControl. If the grid is not the DIRECT child of the TabItem, then it's children will have to be called and so on.
XAML:
<TabControl x:Name="tbControl">
<TabItem>
<Grid x:Name= "firstTabGrid" Tag="0">
<Label Content="label one"/>
<Label Content="label two"/>
</Grid>
</TabItem>
<TabItem>
...
</TabItem>
C#:
foreach (Label l in firstTabGrid.Children)
{
if (l.Content.ToString() == "matching string here")
{
tbControl.SelectedIndex = Convert.ToInt32(firstTabGrid.Tag.ToString());
l.Background = Brushes.Yellow;
}
}
Upvotes: 1