dotnet / dotnet/wpf

FrameworkTemplate.FindName should provide a way to avoid InvalidOperationException

Open
#6,343 2 comments 2 reactions 1 assignee Claimed by @singhashish-wpf View on GitHub
Bug Priority:3
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

The `FrameworkTemplate.FindName` method throws `InvalidOperationException` when the template doesn't match:

https://github.com/dotnet/wpf/blob/559ca9bfc2b47691e9966fcb28c7378e7c1521fc/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs#L317-L320

But there appears to be no way for the caller to check this condition beforehand in order to avoid the exception. The `TemplateInternal` property is internal. And the actual object I have is `ContentPresenter`, whose `TemplateInternal` implementation just returns its `Template` property, which is *private*. That property is a dependency property backed by an internal field. So I appear to be cut off in all ways.

Fix ideas:
1. Add a `TryFindName` method that won't throw. This would solve the whole problem.
2. Expose the `ContentPresenter.Template` property. This would solve *my* problem but doesn't guarantee that the exception can be avoided for all control types.

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.