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

feat: ItemTypeAttribute #13104

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

Conversation

workgroupengineering
Copy link
Contributor

Part of AvaloniaUI/AvaloniaVS#386

What does the pull request do?

Allow AvaloniaVS Intellisense to identify the item type of ItemsControl, ListBox, etc..

What is the current behavior?

AvaloniaVS Intellisense show all class derived from AvaloniaObject

What is the updated/expected behavior with this PR?

How was the solution implemented (if it's not obvious)?

ItemTypeAttribute indicates AvaloniaVS Intellisense what the base type of the items in the ListBoxItem example control is for ListBox.

ItemTypeAttribute derives from DesignerAttribute, to allow in the future in a PR to eliminate all the attributes derived from DesignerAttribute during the packaging phase and keep them only in the reference assemblies.

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

@avaloniaui-team
Copy link
Contributor

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

Comment on lines +26 to 27
[ItemType(typeof(ContentControl))]
public class ItemsControl : TemplatedControl, IChildIndexProvider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ItemsControl can have any types as items. ContentControl is just a default for virtualized containers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ItemType does not add any constraints, it is only used to allow Intellisense to propose the types.

I use this because it is default container that can be used to display an item.

/// <summary>
/// Creates or a container that can be used to display an item.
/// </summary>
protected internal virtual Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
{
return new ContentPresenter();
}

[AttributeUsage(AttributeTargets.Class,
AllowMultiple = false,
Inherited = true)]
public sealed class ItemTypeAttribute: DesignerAttribute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I am not sure about this PR.
Default controls could be guessed from the naming (ListBox -> ListBoxItem) or hardcoded. When this PR assumes that every custom control will apply this attribute to be efficient. Which is unlikely to be followed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ItemTypeAttribute is marked Inherited true. So if you create the public class MyFashionListBox:ListBox, it will inherit the ItemTypeAttribute from the ListBox. Additionally we could create an analyzer/fixer or create a source generator that parses CreateContainerForItemOverride and generates ItemTypeAttribute.

@avaloniaui-bot
Copy link

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

@avaloniaui-bot
Copy link

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

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0050224-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.

None yet

4 participants