✅ Databinding using CommunityTooklit.MVVM
When I am trying to bind to a property on a custom class I am running into some trouble because im not sure how to do it. I am able to get the list to show items using ItemsSource="{Binding Players}" however the items themselves are not showing their properts (Ex: PlayerName) All I see is a blank row.
I have changed line 49 to say PlayerName to try to bind to the property of the actual item in the list but that did not work. Any tutorials or tips?
https://paste.mod.gg/diqyvgttkvos/0
BlazeBin - diqyvgttkvos
A tool for sharing your source code with the world!
23 Replies
It is giving me "Property "PlayerName" not found on MainViewModel". I am not sure how to tell it to look for the property of the item in the observable collection.
That should work I believe?
Should you have the PName / PClass properties on the view model as that's dealing with a collection not an individual PlayerCharacter record?
I have those properties there so I can databind them to the entry boxes to create the playercharacter record
Is that the wrong way to do that?
ah sorry, no that's probably fine
I couldn't see anything wrong with your viewmodel as to why it shouldn't be finding the properties though, worked for me when I copied your code and set out the ListView as above
Wait the code you pasted worked on my original one?
after I updated the xaml snippet yeah
Tried using your code i guess i should specify im using maui because Listview.view isnt working
Ah
Do you want them in separate columns or just the player represented as a single string is fine?
Seperate columns might be better in case i add more properties
Either way im really just putting them in a list view to make sure they are creating properly and to make sure i know how to databind properly
ok, I was going to say you could just have:
<ListView ItemsSource="{Binding Players}" />
then override the ToString() method in your PlayerCharacter class
to return whatever you wantYeah thats one way true. Is there a better way or is that the way youre supposed to do it?
I mean it's A way, all depends on what you want really, and if you're going to be adding properties and wanting to see the data then probably becomes a pain to keep updating what ToString outputs - could you bind it to a DataGrid or something instead?
Though I don't know if MAUI has a DataGrid actually
I dont think it does but im not positive. But it doesnt matter WHAT i bind it to. I can even bind it to a label if needed I just would like to be able to see how to bind to a property of an object in a list (observablecollection).
looking at the docs: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/listview
it should work:
Then yeah what in the world?
Okay let me see if i did something wrong somewhere else then because my example is the same as the docs
Im really not sure, maybe you can find something if you had all the code?
https://github.com/EricAwesomeness/Eldoria
GitHub
GitHub - EricAwesomeness/Eldoria: Text based adventure idle game wi...
Text based adventure idle game with a focus on story/characters - GitHub - EricAwesomeness/Eldoria: Text based adventure idle game with a focus on story/characters
Good News is I fixed it but bad news is I have no idea why?
I removed x:DataType="viewmodel:MainViewModel"
From the xaml and intellisense is giving me errors for all of my bindings now. BUT when i run it it works.
No datacontext found for binding "property" is the error i am getting on the xaml now
hmm have you tried in your xaml:
?
Instead of or with it?
instead of, yeah
That worked flawlessly
Can you explain a bit?
love to take credit but just found it here 😄
https://learn.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm
I've done it similarly using Avalonia
Hmm okay well its fixed for now. I hope to learn more about that if i have to later. Thanks for help for the 2nd time today!