dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

SelectionChangedCommand from CollectionView doesn't work #8792

Closed RobertoGFilho closed 7 months ago

RobertoGFilho commented 2 years ago

Description

SelectionChangedCommand from CollectionView doesn't trigger associated command if you have a frame control into ItemTemplate

    <CollectionView 
    ItemsSource="{Binding Items, Mode=OneWay}"
    SelectedItem="{Binding SelectedItem, Mode=OneWay}"
    SelectionChangedCommand="{Binding SelectCommand, Mode=OneWay}"
    SelectionMode="Single">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="viewModels:CategoryViewModel">
                <Frame Margin="6,3">
                    <StackLayout>
                        <Label 
                        Text="{Binding Model.Name, Mode=OneWay}"
                        FontSize="Title"/>
                    </StackLayout>
                </Frame>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

https://user-images.githubusercontent.com/68563526/179430772-956b41c0-ad10-4dc4-90c6-7f3c290663a5.mp4

Steps to Reproduce

  1. Create a new MAUI Project
  2. Add CollectionView
  3. Add SelectionChangedCommand="{Binding SelectCommand, Mode=OneWay}"
  4. Tap on the Item

Version with bug

6.0.408 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12

Did you find any workaround?

No response

Relevant log output

No response

ghost commented 2 years ago

Hi @RobertoGFilho. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

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.

RobertoGFilho commented 2 years ago

Hi @RobertoGFilho. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

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.

Hi ! You can reproduce the issue, with this sample project: https://github.com/RobertoGFilho/MAUITest

PaulESanchezC commented 2 years ago

Any news on this issue! I am having the same situation!

UwpDev commented 1 year ago

I am also facing this issue. This is basic functionality but MAUI team not even acknowledging this issue is concerning. I faced similar issue but found that it was due to incorrect SelectionChangedCommandParameter after reading this: Stackoverflow After correcting it, it worked fine.

Okuniva commented 1 year ago

Same problem - Button in CollectionView not working

<Button   Command="{Binding ScrollToCommand}"
                 CommandParameter="1" >

 <CollectionView ItemsSource="{Binding GroupIds}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Button Text="{Binding .}"
                    Command="{Binding ScrollToCommand}"
                    CommandParameter="1" />
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

public ObservableCollection GroupIds { get; private set; } = new();

[RelayCommand] async Task ScrollTo(string text) { WriteLine("help...") }

acaliaro commented 1 year ago

I think you have to use relative bindings

https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/relative-bindings?view=net-maui-7.0#bind-to-an-ancestor

dongfo commented 1 year ago

SelectedItem binding and SelectedChanged do not work ,I'm so depressed, is MAUI general available? too much bugs

GoodwayIHung commented 1 year ago

I also ran into same problem. Why does such a basic function fail?

mfranc28 commented 1 year ago

any news? I have same problem. My workaround was to set the SelectedItem property and then in VM make a variable to have the element selected:

<CollectionView ItemsSource="{Binding Action}"
SelectionMode="Single" SelectionChangedCommand="{Binding MissionsCommand}" SelectedItem="{Binding SelectedMenuItem}"

thenin VM:

public HomePageAction selectedMenuItem{get; set;}

[RelayCommand] private void Missions() {
Console.WriteLine(selectedMenuItem.ActionName); }

jfversluis commented 7 months ago

I think this issue now described a couple of different ones. With the limited information and code I see from the different people I think a couple of these are due to understanding how the data binding scope works. If you're inside of a CollectionView (or ListView) then the scope of the databinding changes to the object that is contained in the collection, not the full view model anymore. Maybe this video will help. Although this is about Xamarin, the exact same is true for .NET MAUI.

For the other ones, I can't really tell what might be going on. If you still have issues with this, please open a new issue with all the details for you and ideally a reproduction project attached.