✅ 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
EricAwesomeness
EricAwesomenessOP14mo ago
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.
SinFluxx
SinFluxx14mo ago
<ListView ItemsSource="{Binding Players}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding PlayerName}" />
<GridViewColumn DisplayMemberBinding="{Binding PlayerClass}" />
</GridView>
</ListView.View>
</ListView>
<ListView ItemsSource="{Binding Players}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding PlayerName}" />
<GridViewColumn DisplayMemberBinding="{Binding PlayerClass}" />
</GridView>
</ListView.View>
</ListView>
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?
EricAwesomeness
EricAwesomenessOP14mo ago
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?
SinFluxx
SinFluxx14mo ago
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
EricAwesomeness
EricAwesomenessOP14mo ago
Wait the code you pasted worked on my original one?
SinFluxx
SinFluxx14mo ago
after I updated the xaml snippet yeah
EricAwesomeness
EricAwesomenessOP14mo ago
Tried using your code i guess i should specify im using maui because Listview.view isnt working
SinFluxx
SinFluxx14mo ago
Ah Do you want them in separate columns or just the player represented as a single string is fine?
EricAwesomeness
EricAwesomenessOP14mo ago
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
SinFluxx
SinFluxx14mo ago
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 want
EricAwesomeness
EricAwesomenessOP14mo ago
Yeah thats one way true. Is there a better way or is that the way youre supposed to do it?
SinFluxx
SinFluxx14mo ago
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
EricAwesomeness
EricAwesomenessOP14mo ago
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).
SinFluxx
SinFluxx14mo ago
looking at the docs: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/listview it should work:
<ListView ItemsSource="{Binding Monkeys}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView ItemsSource="{Binding Monkeys}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
EricAwesomeness
EricAwesomenessOP14mo ago
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
EricAwesomeness
EricAwesomenessOP14mo ago
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
EricAwesomeness
EricAwesomenessOP14mo ago
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
SinFluxx
SinFluxx14mo ago
hmm have you tried in your xaml:
<ContentPage.BindingContext>
<viewmodel:MainViewModel />
</ContentPage.BindingContext>
<ContentPage.BindingContext>
<viewmodel:MainViewModel />
</ContentPage.BindingContext>
?
EricAwesomeness
EricAwesomenessOP14mo ago
Instead of
x:DataType="viewmodel:MainViewModel"
x:DataType="viewmodel:MainViewModel"
or with it?
SinFluxx
SinFluxx14mo ago
instead of, yeah
EricAwesomeness
EricAwesomenessOP14mo ago
That worked flawlessly Can you explain a bit?
SinFluxx
SinFluxx14mo ago
blobthumbsup 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
EricAwesomeness
EricAwesomenessOP14mo ago
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!
Want results from more Discord servers?
Add your server