microsoft / microsoft/microsoft-ui-xaml

Subclassing ScrollView won't allow custom templates

Open
#10,233 1 comment 0 reactions 0 assignees View on GitHub
area-ScrollBar area-Scrolling bug team-Controls
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

I was trying to subclass ScrollView, and while that works fine, I'm not able to change `DefaultStyleKey` to my own style from Generic.xaml. When I set it to my own type, OnApplyTemplate is never called and the control fails to render.

### Steps to reproduce the bug

1. Either download and run sample app and skip to step 5: [ScrollViewTemplateBug.zip](https://github.com/user-attachments/files/18120718/ScrollViewTemplateBug.zip)
or:
2. Create a new templated control and inherit from ScrollView.
3. Set the DefaultStyleKey to the type of the control
4. Define a matching style in Generic.xaml (it can just be the default scrollview template with the type parameters changed).
5. Run sample and observe `OnApplyTemplate` is not hit in the custom control (and attached sample renders the red background)
6. Remove the "DefaultStyleKey" setter in the constructor and notice that OnApplyTemplate is not hit and the template from the ScrollView baseclass is applied instead (attached sample now renders green).

### Expected behavior

The custom template is applied so that we can easily attach additional behavior to scrollview

### Screenshots

_No response_

### NuGet package version

WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003

### Windows version

Windows 11 (24H2): Build 26100

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the attached ScrollViewTemplateBug sample and inspect the custom control's Generic.xaml, DefaultStyleKey setup, and OnApplyTemplate behavior. Compare the custom template with the ScrollView base template; done means a subclass can apply its own template and OnApplyTemplate is called.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.