dotnet / dotnet/wpf

Searching for an element (Find Name method) in a template contained in a tabItem not yet displayed gives Nothing.

Open
#10,559 2 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/Searching-for-an-element-Find-Name-meth/10868504)._

---
In a WPF project in VB, I want to display parameter valuescontained in a setting file.
These valuesare visible on different tabItems.
I use the load event of the window to perform this task.

If a tabItem has not yet been displayed, when I search for a control contained in a Control.template of the tabItem, the search control is not found.
On the other hand, the control containing the template is found.

In a test program, I created a button with a template containing a label.
This button is on a tabItem named ‘Test2’.
Using the ‘FindName’ button I use the ‘findname’ method to find the button and label.

On the test program screen you can see:

1. At the window load event, the button is found, the label is not found.
2. After loading the window (tabItem 'Test1' has focus), if I press the ‘FindName’ button, the button is found, the label is not found.
3. After displaying the contents of the tabItem (Test2), the button and label are found.

After the first display of the tabItem (Test2), the button and label will always be found.

![TestFindTabItem.gif](https://aka.ms/dc/image?name=Bd81b1e389ae248a79bbb9729fd7ce16c638772764554617961_TestFindTabItem.gif&tid=d81b1e389ae248a79bbb9729fd7ce16c638772764554617961)

```
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestFind"
mc:Ignorable="d"
Title="MainWindow" Height="225" Width="480" Loaded="Window_Loaded">

<Grid Margin="0,0,0,-1">

<TabControl x:Name="tbcTest" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Transparent" BorderBrush="#FF3B333B" Height="164" Width="240" Margin="6,9,0,0" >
<TabItem x:Name="tbiTest1">
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="14" Foreground="Red" Margin="3,0" Height="22" Text="Test1"/>
</StackPanel>
</TabItem.Header>
</TabItem>

<TabItem x:Name="tbiTest2">
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="14" Foreground="Red" Margin="3,0" Height="22" Text="Test2"/>
</StackPanel>
</TabItem.Header>

<StackPanel Orientation="Horizontal">
<Button x:Name="btnTest" FontSize="14" BorderThickness="0">
<Button.Template>
<ControlTemplate x:Name="ctpTest" TargetType="{x:Type Button}">
<Border x:Name="BorderButton" CornerRadius="8" Background="#FF3B333B">
<Label Content="Test" x:Name="lblTest" Foreground="White"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</TabItem>
</TabControl>
<Button x:Name="btnFindName" Content="FindName" HorizontalAlignment="Left" Margin="280,24,0,0" VerticalAlignment="Top" Width="73"/>
<Label Content="Find button :" HorizontalAlignment="Left" Margin="277,79,0,0" VerticalAlignment="Top"/>
<Label Content="Find Label (Template) :" HorizontalAlignment="Left" Margin="277,98,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblFindButton" Content="No" HorizontalAlignment="Left" Margin="409,79,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblFindLabel" Content="No" HorizontalAlignment="Left" Margin="409,98,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>

``````
Class MainWindow

Private Sub Window\_Loaded(sender As Object, e As RoutedEventArgs)

btnFindName\_Click(Nothing, Nothing)

End Sub

Private Sub btnFindName\_Click(sender As Object, e As RoutedEventArgs) Handles btnFindName.Click

Dim button As Button = CType(tbiTest2.FindName(“btnTest”), Button)
If button Is Nothing Then
lblFindButton.Content = “No”
Else
lblFindButton.Content = “Ok”
End If

Dim label As Label = CType(btnTest.Template.FindName(“lblTest”, btnTest), Label)
If label Is Nothing Then
lblFindLabel.Content = “No”
Else
lblFindLabel.Content = “Ok”
End If

End Sub

End Class

```

Here is the test program.
[TestFind.zip](https://aka.ms/dc/file?name=Bda6a14002d094c6997ec3688a61cb16d638772767491012488_TestFind.zip&tid=da6a14002d094c6997ec3688a61cb16d638772767491012488)

---
### Original Comments

#### Feedback Bot on 3/11/2025, 03:46 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.