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

Issue with bindings in ListView #20883

Closed
cntseesharp opened this issue Feb 28, 2024 · 3 comments
Closed

Issue with bindings in ListView #20883

cntseesharp opened this issue Feb 28, 2024 · 3 comments
Labels
area-controls-listview ListView and TableView platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working

Comments

@cntseesharp
Copy link

Description

Command="{Binding BindingContext.DeleteNoteCommand, Source={RelativeSource AncestorType={x:Type vm:NotesOverviewViewModel}}}" 
CommandParameter="{Binding .}"
[RelayCommand]
private async Task DeleteNote(NoteModel note)

This binding completely crashes element creation when CommandParameter is used, because ListView on initializing a ViewCell sets its BindingContext from the parent, instead of the ItemsSource element.

Step by step:

  1. ViewCell is created.
  2. Its BindingContext is set to the ListView BindingContext.
    --- During this step binding a command to a VM property will crash due to the wrong CommandParameter type.
  3. Something happens and BindingContext is changed to the item from ItemsSource.

This mistake is also the reason why everyone sees binding errors even though their bindings are working in the end.

Steps to Reproduce

  1. New MAUI app or existing one
  2. Create a data model
public partial class NoteModel : ObservableObject
{
    [ObservableProperty]
    private Guid _id;
}
  1. In your ViewModel create a collection of your model
private ObservableCollection<NoteModel> _notes = [];
public ObservableCollection<NoteModel> Notes => _notes;
  1. In your ViewModel create a method that takes your model as an argument
[RelayCommand]
private async Task DeleteNote(NoteModel note)
{
    Notes.Remove(note);
}
  1. In your XAML create binding to the method inside the ViewCell
<ListView ItemsSource="{Binding Notes}" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="notes:NoteModel">
            <ViewCell>
                <Grid>
                    <Button Command="{Binding DeleteNoteCommand, Source={RelativeSource AncestorType={x:Type vm:NotesOverviewViewModel}}}" CommandParameter="{Binding .}" />
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
  1. Get the following exception.
    image

HOTFIX:

Use objects as arguments, to prevent the exception, cast your data manually

[RelayCommand]
private async Task DeleteNote(object? obj)
{
    if (obj is not NoteModel selectedNote)
        return;

    Notes.Remove(selectedNote);
}

Link to public reproduction project repository

No response

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

19045.3930

Did you find any workaround?

Use object as arguments, to prevent the exception, and cast your data manually

[RelayCommand]
private async Task DeleteNote(object? obj)
{
    if (obj is not NoteModel selectedNote)
        return;

    Notes.Remove(selectedNote);
}

Relevant log output

No response

@cntseesharp cntseesharp added the t/bug Something isn't working label Feb 28, 2024
@hugebug4ever
Copy link

hugebug4ever commented Mar 17, 2024

I had the same problem.
It seems caused by the CommandParameter.
And the same code works on iOS. It's should be windows specific problem.

I think this problem is related.
#14234

@kevinxufei
Copy link

Verified this issue with Visual Studio 17.10.0 Preview 2(maui version 8.0.10). Cannot repro this issue.
MauiApp15.zip

@kevinxufei kevinxufei added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Mar 20, 2024
Copy link
Contributor

Hi @cntseesharp. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 27, 2024
@samhouts samhouts added the area-controls-listview ListView and TableView label Aug 27, 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 platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants