Tomas
Topic Author
Posts: 10
Joined: 29 Sep 2024, 18:27

Binding ObservableCollection in version 3.2.7

23 Jan 2025, 10:31

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?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Binding ObservableCollection in version 3.2.7

24 Jan 2025, 11:15

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.
 
Tomas
Topic Author
Posts: 10
Joined: 29 Sep 2024, 18:27

Re: Binding ObservableCollection in version 3.2.7

24 Jan 2025, 11:28

Ok,
Thanks
 
KeldorKatarn
Posts: 243
Joined: 30 May 2014, 10:26

Re: Binding ObservableCollection in version 3.2.7

24 Jan 2025, 21:05

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)
 
Tomas
Topic Author
Posts: 10
Joined: 29 Sep 2024, 18:27

Re: Binding ObservableCollection in version 3.2.7

27 Jan 2025, 18:10

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.
 
KeldorKatarn
Posts: 243
Joined: 30 May 2014, 10:26

Re: Binding ObservableCollection in version 3.2.7

30 Jan 2025, 14:22

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/

Who is online

Users browsing this forum: Bing [Bot] and 2 guests