FrameworkTemplate.FindName should provide a way to avoid InvalidOperationException
- 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
Assessment
This issue has not been assessed yet.