TemplateBinding of custom control dependency properties does not get applied when style is imported from App.xaml
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 296
Description
Using the standard .NET MAUI template in VS2022, version 17.3.6.
.NET Core version is 6.0
Development environment is Window 10 64 bit.
**Steps to reproduce:**
1. Create a Control class inheriting from TemplatedView (e.g. InputView). Add at least one dependency property (e.g. Text, Label).
2. Inside Styles.xaml (resource dictionary imported in App.xaml, built into the project template), declare a new style with TargetType="local:InputView" where local is a namespace alias pointing to the root namespace.
3. Inside the style, add a setter for the control template and create a basic layout (Grid, VerticalStackPanel).
4. Make sure to apply templateBinding to bind dependency property values to the control template
5. On the MainPage, add an instance of the custom control, and make sure to set the dependency properties (e.g. text, label)
6. Run The project (does not matter on which platform)
**Result:** Template binding not applied and no binding errors appear. The control template does get applied, but custom dependency properties are NOT visible.
**Workround:** Move the style to the MainPage resources section and re-run the project. The binding will now be applied and the custom dependency properties become visible.
Another (strange) workaround is to apply some changes (any changes, e.g add another container around the main layout) to the style with hot-reload while debugging. This will refresh the view and the templateBinding will now work!
An example solution is attached.
[TemplateBindingNotAppliedBug.zip](https://github.com/dotnet/maui/files/9854746/TemplateBindingNotAppliedBug.zip)
Contributor guide
Research direction
Start by running the attached TemplateBindingNotAppliedBug.zip reproduction and compare the custom control resources in Styles.xaml with the equivalent resources in MainPage and App.xaml. Trace why the control template is applied but bindings to the custom dependency properties are not, then verify that the properties appear when the style remains imported from App.xaml and that the workaround is no longer needed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- frontend, mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100