Page 1 of 1

Binding ObservableCollection in version 3.2.7

Posted: 23 Jan 2025, 10:31
by Tomas
Hi,
Binding ObservableCollection<NodeComponent> to ObservableCollection<BaseComponent> in version 3.2.7 does not work
struct NodeComponent: public BaseComponent {};
How to fix this correctly for version 3.2.7?

Re: Binding ObservableCollection in version 3.2.7

Posted: 24 Jan 2025, 11:15
by sfernandez
Hi,

In previous versions there was a bug that made all observable collections share the same type, which will incorrectly allow you to cast between unrelated types.

Although it might seem that ObservableCollection<NodeComponent> and ObservableCollection<BaseComponent> are related, that is not the case because they don't inherit one from the other. So either you define your control property to use the same type as the bound property or a base type like BaseObservableCollection. Or make sure that the bound property returns a collection of the same type as the control property.

Re: Binding ObservableCollection in version 3.2.7

Posted: 24 Jan 2025, 11:28
by Tomas
Ok,
Thanks

Re: Binding ObservableCollection in version 3.2.7

Posted: 24 Jan 2025, 21:05
by KeldorKatarn
Ok,
Thanks
As a side note: While not recommended, if you have a use-clase where you absolutely NEED to change the type of the collection items, check github. There's some implementations for synchronized ObservableCollection where you can pass a Func that converts one type to another and the collection will keep itself in sync with the source collection (edge case use case though, it's best to keep the types identical if at all possible)

Re: Binding ObservableCollection in version 3.2.7

Posted: 27 Jan 2025, 18:10
by Tomas
As a side note: While not recommended, if you have a use-clase where you absolutely NEED to change the type of the collection items, check github. There's some implementations for synchronized ObservableCollection where you can pass a Func that converts one type to another and the collection will keep itself in sync with the source collection (edge case use case though, it's best to keep the types identical if at all possible)
Ok, Thank you for your answer.

Re: Binding ObservableCollection in version 3.2.7

Posted: 30 Jan 2025, 14:22
by KeldorKatarn
As a side note: While not recommended, if you have a use-clase where you absolutely NEED to change the type of the collection items, check github. There's some implementations for synchronized ObservableCollection where you can pass a Func that converts one type to another and the collection will keep itself in sync with the source collection (edge case use case though, it's best to keep the types identical if at all possible)
Ok, Thank you for your answer.
Specifically these might be useful to you: https://github.com/Cysharp/ObservableCollections/