Generate properties for templates using TemplatePartAttribute
See original GitHub issueThere is a new attribute ported from WPF. It will be useful to have optional code generation for “parts” of templates. As an example, user code:
[TemplatePart("PART_TextPresenter", typeof(TextPresenter))]
public partial class TextBox
{
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (IsFocused)
{
TextPresenter?.ShowCaret(); // TextPresenter is generated
}
}
}
Additional generated code, adds new property and sets value to it:
#nullable enable
public partial class TextBox
{
private TextPresenter? TextPresenter { get; set; }
static TextBox()
{
TemplateAppliedEvent.AddClassHandler<TextBox>((control, args) =>
{
control.TextPresenter = args.NameScope.Get<TextPresenter>("PART_TextPresenter");
});
}
}
Problems:
- Only one static ctor can be created for class, if we want to handle TemplateAppliedEvent to fill these properties.
- Only one override of OnTemplateApplied method is possible, if we want to use it to fill these properties.
Which means, we most likely will need to modify Avalonia to add yet another way to read namescope after template was applied.
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:9 (9 by maintainers)
Top Results From Across the Web
How to use the TemplatePart attribute on a custom control?
It seems that the TemplatePart attribute is not required to use the template part in code (using the GetTemplateChild() method), I can get...
Read more >Add TemplatePartAttribute for Controls · Issue #7432
Named parts in control templates are not always clear to control ... Generate properties for templates using TemplatePartAttribute #11934.
Read more >Further TemplatePart Cleanup in Code-Behind and XAML
Mentioned here: Generate properties for templates using TemplatePartAttribute #11934. Describe alternatives you've considered.
Read more >PART Control Template and TemplatePartAttribute
The only thing we have to do it get the custom attributes of type TemplatePartAttribute. Here is simple code of it. Code Snippet....
Read more >TemplatePartAttribute Class (System.Windows)
Represents an attribute that is applied to the class definition to identify the types of the named parts that are used for templating....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

There was a lot of further discussion in https://github.com/AvaloniaUI/Avalonia/issues/9093 related to how to define the string constants and then enforce them statically in XAML. I think that discussion isn’t fully closed yet and it may influence what is done here. Feel free to jump into that conversation as well. That said, the TemplatePart attribute isn’t going to change so however the string name gets into the attribute is irrelevant for you.
Concerning the code generation:
TextPresenterthe control should be named justText. Not all template parts currently have good names…Edit:
Regarding the implementation details of the new generator, I guess it is worth introducing a new class similar to
AvaloniaNameGeneratornamed e.g.TemplatePartGenerator, then adding a new option toGeneratorOptionsthat would control if the new generator is enabled, and then invoking the generator in the composition root.The code that scans the assembly and finds all classes annotated with the
TemplatePartattribute can be found here https://github.com/reactivemarbles/ObservableEvents/blob/main/src/ReactiveMarbles.ObservableEvents.SourceGenerator/SyntaxReceiver.cs#L17 Most likely we’ll need to implementISyntaxReceiverthat receives all relevant classes withTemplatePartattributes.