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.5k stars 2.21k forks source link

TreeDataTemplate ItemsSource binding Converter #10379

Open timunie opened 1 year ago

timunie commented 1 year ago

Discussed in https://github.com/AvaloniaUI/Avalonia/discussions/10347

Originally posted by **PashtetR** February 15, 2023 Hi, Everyone! I want to save some approaches, porting my WPF application to AvaloniaUI. I used binding with converter to set ItemsSource in HierarchicalDataTemplate, according to DataType in WPF. Something like this: `HierarchicalDataTemplate DataType="{TagGroup}" ItemsSource="{Binding Converter={StaticResource TreeItemsConverter}}"` I want apply the same practice. Something like this: `TreeDataTemplate DataType="{TagGroup}" ItemsSource="{Binding Converter={StaticResource TreeItemsConverter}}"` TagGroup is my model class. TreeItemsConverter returns ObservableCollection. Unfortunately converter isnt called. There is a message in output-window: `[[Binding]Binding produced invalid value for 'Items' ('System.Collections.IEnumerable'): 'GroupName' ('Project Model.Groups.TagGroup')(TreeViewItem #55094571) ` Other converters with other bindings are working. This converter with TreeView.Items binding (for example) is working too. Can you help me? Is it a bug or feature? Thanks!
timunie commented 1 year ago

I had a deeper look. Seems to be an issue in Avalonia, where the Converter is ignored internally. This line seems to be the root cause:

https://github.com/AvaloniaUI/Avalonia/blob/e7e04a067fc8fa7b71ca4256efb81f5cb536ee18/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs#L37-L53

gusmanb commented 1 year ago

Any news on this issue? This prevents the proper usage of the treeview when using heterogeneous data structures.

timunie commented 1 year ago

I guess that my initial idea would result in a breaking change (see Draft PR) which is not acceptable for the 11.x time frame. Maybe @grokys has an idea?

Tyrrrz commented 5 months ago

@timunie is there a way for me to manually hack a custom TreeDataTemplate that supports binding converters? I can implement my own ITreeDataTemplate, but I don't know what I have to do with ItemsSelector for it to work with a converter.