Warning AVLN2208: Item container in the data template #18132
+199
−39
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does the pull request do?
It's a fairly common mistake to put item container inside of data template.
Typical example can be found even in this repository:
Avalonia/src/Avalonia.Diagnostics/Diagnostics/Views/EventsPageView.xaml
Lines 74 to 80 in 151d97a
The problem with this code is that DataTemplate does not affect how container itself is defined. Instead, it will create a dumb ListBoxItem inside of the proper ListBoxItem. Making it worse, styles are active for both ListBoxItems, but only outer one can be actually selected.
Example of how this code looks in the elements tree:
![image](https://private-user-images.githubusercontent.com/3163374/410262893-96ac2771-9279-4aa9-ae36-1cf80e09eb77.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4MzI4MTcsIm5iZiI6MTczODgzMjUxNywicGF0aCI6Ii8zMTYzMzc0LzQxMDI2Mjg5My05NmFjMjc3MS05Mjc5LTRhYTktYWUzNi0xY2Y4MGUwOWViNzcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDZUMDkwMTU3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OTk1YTYyYWU4YzMxYTc5ZjJiZWUxNmY2YmVjODZmYjBhY2M4M2Y3Yzk3NWUzZjA5NmFkYzBhZGE0OGIwNmE0YSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.0ItmpBNTMX0jXI9GthY4U5fyLjEvlQtoOUZ5JK-W1F0)
This problem is not limited by ListBoxItem, but by any items control. I often see this mistake with MenuItem and TabControl.
What is the updated/expected behavior with this PR?
New XAML analyzer is added to XamlX compiler.
Code: "AVLN2208" (can be configured as an error via project settings or editorconfig, or disabled).
Message example (actual text depends on the context):
Checklist
Breaking changes
By default, this is a warning. No user code will have these errors, unless these projects have all warnings enabled as errors - but these projects typically expect new errors to be found early.
Fixed issues
Contributes towards #13707