0

I am using .net MAUI's <CollectionView> with a nested <CollectionView.ItemTemplate> to bind to a collection of objects. I understand how to define the item template so that it contains Label objects that bind to properties of the objects in the collection: using (for example) Text="{Binding Surname}". This works correctly.

I'd also like to bind to a collection of objects that expose readonly member variables, not properties, but binding by name does not result in a value being displayed. No runtime errors occur.

Is there a way to bind collection view items to member variables?

(Using VS 2022 Preview 7.4.0 with .net 6)

1
  • binding only works with public properties Commented Aug 23, 2022 at 15:40

2 Answers 2

3

Is there a way to bind collection view items to member variables?

No, it only works with public properties. This is simply the way it's implemented. The XAML binding engine will go through the object that serves as a binding context and will look for public properties only.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'll use a proxy class to front the member variables as properties.
1

A work-around is to add a property that returns the member variable.

Given MyType MyVariable;, add:
public MyType MyProperty => MyVariable;

That is shorthand for public MyType MyProperty { get { return MyVariable; } }.

2 Comments

Thanks. I can't change the class that contains the member variables, but I'll add a separate class that proxies them as properties.
You might want to be careful with these though, depending on what you're doing: dotnetfiddle.net/DRN53X

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.