Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning AVLN2208: Item container in the data template #18132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

maxkatz6
Copy link
Member

@maxkatz6 maxkatz6 commented Feb 6, 2025

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:

<ListBox Name="EventsList" ItemsSource="{Binding RecordedEvents}"
SelectedItem="{Binding SelectedEvent, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Classes.handled="{Binding IsHandled}">
<Grid ColumnDefinitions="Auto,Auto,Auto,*,Auto">

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

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):

Unexpected 'TabItem' inside of 'TabControl.DataTemplates'. 'TabControl.DataTemplates' defines template of the container content, not the container itself.

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

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.3.999-cibuild0054805-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants