Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTemplate bindings do not work #21481

Closed
AlphaNERD- opened this issue Mar 27, 2024 · 3 comments
Closed

DataTemplate bindings do not work #21481

AlphaNERD- opened this issue Mar 27, 2024 · 3 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Milestone

Comments

@AlphaNERD-
Copy link

Description

Helllo again .NET MAUI dev team,

i have discovered another issue with bindings in .NET MAUI: When trying to bind to a property within a DataTemplate, i do not get the value of the property i bound to. Instead i get the ToString() output of the object whose property i wanted to make a binding to.

I've created a WPF version of this demo project to ensure that i created the bindings correctly: https://github.com/AlphaNERD-/WPFBindingDemo/

Steps to Reproduce

  1. Build the project for Android (See issue Invoking the PropertyChanged event throws NullReferenceException #21431 for the reason why)
  2. Enter something within the entry. Ideally one of the first or last names listed within the constructor of ViewModelTest

Expected result:
You should see a list of names appear under the entry.

Actual result:
You see a list of "BindingDemoForGithub.ViewModelTest+TestUser" appear under the entry.

Link to public reproduction project repository

https://github.com/AlphaNERD-/NETMAUIBindingDemo

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 14

Did you find any workaround?

One way we could get around this is to use ToString(). This will help users working with VERY simple lists.

Relevant log output

No response

@AlphaNERD- AlphaNERD- added the t/bug Something isn't working label Mar 27, 2024
@PureWeen PureWeen added the area-xaml XAML, CSS, Triggers, Behaviors label Mar 27, 2024
@PureWeen PureWeen added this to the Backlog milestone Mar 27, 2024
@mattleibow
Copy link
Member

I think this is because ListView actually needs to use a ViewCell:

<ListView ItemsSource="{Binding TestUsers, Mode=TwoWay}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding UserName}"/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Maybe this is a result of the sample app... But, another thing I notice is that you have a list view in a vertical stack and that is in a scroll view. Doing this will cause the entire list to load all views - which will disable virtualization. You probably need to use a Grid and do not put a virtualized control (listview/collection view) in a infinite control - stack layout and scroll view. Nested scrolling controls in the same direction effectively loads all views at startup.

@mattleibow mattleibow added the s/needs-info Issue needs more info from the author label Mar 27, 2024
@AlphaNERD-
Copy link
Author

Adding the <ViewCell> tags fixed the binding issue. I do wonder though: Why do i need to use the <ViewCell> tags in the first place?

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels Mar 28, 2024
@jfversluis
Copy link
Member

@AlphaNERD- that is the way it was setup for a ListView, all cells have to inherit from ViewCell also see the docs, this was changed for CollectionView.

Glad you got this to work!

@github-actions github-actions bot locked and limited conversation to collaborators May 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants