Home > Software engineering >  .NET MAUI Communitytoolkit.Mvvm: How to bind an object instead of a string
.NET MAUI Communitytoolkit.Mvvm: How to bind an object instead of a string

Time:01-19

i already the project from the official tutorial of .NET MAUI until step 5 using Communitytoolkit.Mvvm: enter image description here

where the content page is declared as this:

enter image description here

Remark: The MainViewModel looks like this:

enter image description here

  • Should i declare some uses, or namespace?
  • where in the project should i place the Class of the objects i would bind?

Thanks in advance, Thomas

Also tryed to implement the class ItemGroup in the MainViewModel.cs but then i had any more problems with access to this class.

CodePudding user response:

Try this

<Label Text="{Binding name}" …

Note that name must be a public property

CodePudding user response:

I believe your problem lies with you Binding declaration inside you ItemTemplate. Given that you bound your CollectionView's ItemSource to "items", the ItemTemplate's data context is now a single itemGroup from your list. Therefore, you should not be writing

<CheckBox IsChecked="{Binding ItemGroup.IsChecked}"[...]

But instead

<CheckBox IsChecked="{Binding IsChecked}"[...]

Since you are already "within" a single itemgroup object. Same goes for your Label. Furthermore, I don't think you actually need to declare the Datatype for your item's DataTemplate.

p.s. Watch out for your item declaration, it seems like you have both items and Items declared in your ViewModel; and both seem public.

CodePudding user response:

You have a number of issues here.

The community mvvm toolkit includes code generators.

A variable:

[ObservableProperty]    
string text;

Is examined by the code generator at compile time and in a partial class it will add a public property Text.

Binding is case sensitive and you need to bind to public properties so you need to use upper case on that first letter. Binding Text rather than Binding text.

You might have other instances where you did the same sort of thing.

I don't see where you use it but that add [Relaycommand] will generate AddCommand as a public property which you bind to.

Additionally you have misunderstood how an itemscontrol gets an item out a bound collection per row. As DevenCC points out, once you bind ItemsSource to Items then each row will get an instance of whatever is in that collection so you should bind IsChecked rather than ParentProperty.IsChecked.

As to where to put classes. In large apps it is a nuisance to flip between a folder contains views and another contains viewmodels. You might want to consider a folder contains each pair of view and viewmodel. Hence a foo folder containing fooView and fooViewModel

  • Related