AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.88k stars 2.24k forks source link

Respect data template for ItemsControl container generation. #10745

Open rabbitism opened 1 year ago

rabbitism commented 1 year ago

Is your feature request related to a problem? Please describe. I see many users complain about setting container styles according to data, for example, setting listboxitem background according to data. This can be achieved by setting ItemContainerTheme with properties bind to datacontext, however this is not intuitive, and not friendly to compiled binding especially when designing new collection controls for displaying complex data.

Describe the solution you'd like Add a new bool property like "UseItemTemplateForContainer", false by default. If this is set to true, then items will be built with ItemTemplate first, and then go through the process of checking if itself is container, and do the container generation/preparation. In this way, we can use ListBoxItem directly as root of ItemItemplate, without making a listboxitem inside another listboxitem. And it will have no impact on existing code.

Describe alternatives you've considered

Additional context

amwx commented 1 year ago

FWIW, WinUI supports this implicitly. Newer controls like the TabView create the DataTemplate as a TabItem which is then used as a container, which is something I completely forgot about in the discussion around the new items control. Granted they don't have the hierarchy of DataTemplates to search here so I think a property to control it is reasonable.

image

thexamlguy commented 1 year ago

FWIW, WinUI supports this implicitly. Newer controls like the TabView create the DataTemplate as a TabItem which is then used as a container, which is something I completely forgot about in the discussion around the new items control. Granted they don't have the hierarchy of DataTemplates to search here so I think a property to control it is reasonable.

image

Something similar to this property in WPF? I think the Menu and StatusBar are the only controls that has a property for this purpose too.