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

Databinding errors on ListView - What am I doing wrong? #19873

Closed
enjoysmath opened this issue Jan 12, 2024 Discussed in #10277 · 5 comments
Closed

Databinding errors on ListView - What am I doing wrong? #19873

enjoysmath opened this issue Jan 12, 2024 Discussed in #10277 · 5 comments
Labels
area-controls-listview ListView and TableView area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging area-xaml XAML, CSS, Triggers, Behaviors s/move-to-vs-feedback Tells automation to ask the person to use VS Feedback to report the issue

Comments

@enjoysmath
Copy link

ListView ItemTemplate Data-binding currently gets these Errors at run-time and also the styling looks way off:

https://stackoverflow.com/q/77809597/7076615

I can't switch to CollectionView for unrelated / independent other error(s).

Discussed in #10277

Originally posted by danielcooper-hk September 22, 2022
Hi everyone,

I am pulling what little hair I have left out over this - if someone can point me in the right direction I would be very grateful.

When the application runs there are no binding errors but when I click on the button to run the AddItemCommand the item appears in the list view but I get data binding errors on both Name and Description. What am I doing wrong in the ListView to cause this?

Thanks,

Dan.

I've written the following sample:

My MainPageModel:

namespace ListViewMVVM.Models;

public class MainPageModel
{
    public string Name { get; set; } = string.Empty;

    public string Description { get; set; } = string.Empty;
}

My MainPageViewModel:

namespace ListViewMVVM.ViewModels;

public class MainPageViewModel 
{
    public ICommand AddItemCommand { get; private set; }

    private ObservableCollection<MainPageModel> myItems;
    public ObservableCollection<MainPageModel> MyItems
    {
        get { return myItems; }
        set
        {
            if (myItems == value) return;

            myItems = value;
        }
    }

    public MainPageViewModel()
    {
        myItems = new()
        {
            new Models.MainPageModel { Name = "Item 1", Description = "Description 1" },
            new Models.MainPageModel { Name = "Item 2", Description = "Description 2" },
            new Models.MainPageModel { Name = "Item 3", Description = "Description 3" }
        };

        AddItemCommand = new Command(AddRecord);
    }

    public void AddRecord()
    {
        myItems.Add(new Models.MainPageModel
        {
            Name = "Item" + (myItems.Count + 1).ToString(), Description = "Description " + (myItems.Count + 1).ToString()
        }) ;

    }
}

My MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodels="clr-namespace:ListViewMVVM.ViewModels"
             xmlns:models="clr-namespace:ListViewMVVM.Models"
             x:Class="ListViewMVVM.MainPage">

   <ContentPage.BindingContext>
        <viewmodels:MainPageViewModel/>
    </ContentPage.BindingContext>
    
    <ScrollView>
        <VerticalStackLayout>

            <ListView x:Name="ListViewItems"
                      ItemsSource="{Binding MyItems}">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="models:MainPageModel">
                        <TextCell Text="{Binding Name}"
                      Detail="{Binding Description}" />
                    </DataTemplate>
                </ListView.ItemTemplate>


            </ListView>

            <Button
                Text="Add Next"
                Command="{Binding AddItemCommand}"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

And finally the MainPage.xaml.cs:

namespace ListViewMVVM;

public partial class MainPage : ContentPage
{
	public MainPage()
	{
		InitializeComponent();
	}
}
@drasticactions
Copy link
Contributor

If you're seeing errors in the Visual Studio UI but the actual MAUI UI looks fine, it could be an issue with the tooling. @spadapet What do you think?

@enjoysmath
Copy link
Author

Well the whole structure of my app just got amended in design. So now we're doing everything with static pages, and a link to the parent view model is precisely what I need. So you can either fix or close this :)

@jsuarezruiz jsuarezruiz added area-xaml XAML, CSS, Triggers, Behaviors area-controls-listview ListView and TableView area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging s/move-to-vs-feedback Tells automation to ask the person to use VS Feedback to report the issue labels Jan 15, 2024
@ghost
Copy link

ghost commented Jan 15, 2024

Thanks for the issue report @enjoysmath! This issue appears to be a problem with Visual Studio, so we ask that you use the VS feedback tool to report the issue. That way it will get to the routed to the team that owns this experience in VS.

If you encounter a problem with Visual Studio, we want to know about it so that we can diagnose and fix it. By using the Report a Problem tool, you can collect detailed information about the problem, and send it to Microsoft with just a few button clicks.

  1. Go to the Visual Studio for Windows feedback tool or Visual Studio for Mac feedback tool to report the issue
  2. Close this bug, and consider adding a link to the VS Feedback issue so that others can follow its activity there.

@spadapet
Copy link
Contributor

If you're seeing errors in the Visual Studio UI but the actual MAUI UI looks fine, it could be an issue with the tooling. @spadapet What do you think?

I think this is the same bug as:

The binding failures UI in VS shows that MAUI's ListView is temporarily using the wrong data context when new items are added. The data context is supposed to be MainPageModel, not MainPageViewModel.

image

The correct data context will get set on the new items, but the initial error is still logged in the binding failures UI in VS. I'd guess it's a bug in ListView and not a problem with Visual Studio. Using the VS feedback tool isn't going to help more than the main MAUI issue.

cc: @drasticactions

@drasticactions
Copy link
Contributor

Okay, I wasn't sure of the context. We can close this as a duplicate of #8433.

@drasticactions drasticactions closed this as not planned Won't fix, can't repro, duplicate, stale Jan 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-listview ListView and TableView area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging area-xaml XAML, CSS, Triggers, Behaviors s/move-to-vs-feedback Tells automation to ask the person to use VS Feedback to report the issue
Projects
None yet
Development

No branches or pull requests

4 participants