Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 9, 2025

Summary

This PR addresses issue feedback requesting clarification about the behavior of FindName() with dynamically added UI elements in WPF.

Changes

Added a NOTE block after the code example in how-to-find-an-element-by-its-name.md to clarify that:

  • The FindName() method only finds elements that are part of the XAML namescope
  • Elements added programmatically after XAML is loaded don't automatically register in the XAML namescope
  • To make dynamically added elements findable by name, developers must call RegisterName() before adding the element to the parent container
  • Includes a cross-reference to the XAML Namescopes article for additional context

Context

The original implementation example only demonstrated finding elements declared in XAML. Users may not be aware that dynamically instantiated elements (created in code) require explicit registration using RegisterName() to be findable via FindName(). Without this clarification, developers might encounter null references when attempting to find programmatically added elements.

Related Documentation

Fixes feedback on documentation issue describing the limitation.

Original prompt

This section details on the original issue you should resolve

<issue_title>Suggested Addition for "How to: Find an Element by Its Name - WPF .NET Framework"</issue_title>
<issue_description>

Type of issue

Other (describe below)

Description

The example's implementation of the Find() method will only find a child element for the specified name that has been added to the Window's or Page's Content as a non-dynamic element. By default, a dynamic UI Element (e.g., the WPF Framework UIElement was instantiated and and added to the Content of the main view programmatically and not through a XAML declaration) is not added as a NameScoped mapped object, and any calls to FindName() using a dynamic object's Name will return a null object reference.

If a FrameworkElement has been added to the Content dynamically, and you would still need to get a reference to the named element, the elements name should be registered to the NameScoped object map by calling the FrameworkElement.RegisterName() method before the element is added to the parent container for a Window or Page's Content.

See Also:
FrameworkElement.RegisterName(String, Object) Method
https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.registername?view=windowsdesktop-5.0

Page URL

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/how-to-find-an-element-by-its-name

Content source URL

https://github.com/dotnet/docs-desktop/blob/main/dotnet-desktop-guide/wpf/advanced/how-to-find-an-element-by-its-name.md

Document Version Independent Id

62e38876-edf9-8645-cd38-73c20cc0e16c

Article author

adegeo

Metadata

  • ID: 62e38876-edf9-8645-cd38-73c20cc0e16c
  • Service: dotnet-framework
  • Sub-service: wpf

</issue_description>

<agent_instructions>This issue is describing a behavior of Find that users may not be aware of. Make the article clearer to that behavior by adding a note after the code example.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@adegeo Thank you for this information. I'll flag this for updating.

This behavior should be a note added after the code example.</comment_new>

Fixes #1202


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.


Internal previews

📄 File 🔗 Preview link
dotnet-desktop-guide/wpf/advanced/how-to-find-an-element-by-its-name.md dotnet-desktop-guide/wpf/advanced/how-to-find-an-element-by-its-name

Co-authored-by: adegeo <67293991+adegeo@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Update Find method for dynamic WPF elements Add note about FindName behavior with dynamically added elements Oct 9, 2025
@Copilot Copilot AI requested a review from adegeo October 9, 2025 22:17
Copilot finished work on behalf of adegeo October 9, 2025 22:17
@adegeo adegeo marked this pull request as ready for review October 9, 2025 22:47
@adegeo adegeo enabled auto-merge (squash) October 9, 2025 22:47
@adegeo adegeo merged commit 036e040 into main Oct 10, 2025
12 checks passed
@adegeo adegeo deleted the copilot/update-find-method-implementation branch October 10, 2025 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggested Addition for "How to: Find an Element by Its Name - WPF .NET Framework"

3 participants