Search code examples
collectionviewmaui

.net maui MVVM Binding a SelectedItemCommand and SelectedItemParameter from a CollectionView


So I am working with SQLite, CommunityToolkit.Mvvm.ComponentModel;

I have database containing a table of friends. I can bind this to a CollectionView. I am following https://www.youtube.com/watch?v=8_cqUvriwM8 but trying to use MVVM approach.

I can get it to work happily with SelectionChanged and an event, but not with SelectionChangedCommand and I can't get access to the Friend item in the list.

Here is the relevant xaml

    <CollectionView Grid.Row="2"
                    x:Name="FriendsList"
                    SelectionMode="Single"
                    SelectionChangedCommand="{Binding SelectionChangedCommand}" 
                    SelectionChangedCommandParameter="{Binding .}"
                    SelectionChanged="OnSelectionChanged" >

Here is the relevant part of the code (I'm using the code behind for the xaml just for testing)

    public MainPage()
    {
        InitializeComponent();

    this.BindingContext = this;  //cool for binding the xaml to the code behind.
  }
...


//This works fine (so why do I bother with mvvm?)
  public void OnSelectionChanged(Object sender, SelectionChangedEventArgs e)
  {
    Console.WriteLine("Selection changed click");
    Friend f = e.CurrentSelection[0] as Friend;
    Console.WriteLine(f.LName);
  }

//Can't get this to work, though it will register the click
  public ICommand SelectionChangedCommand => new Command(SelectionChangedControl);
  public void SelectionChangedControl()
  {
    Console.WriteLine("selection made");

  }

My thinking was that if I could do this to get at the Friend item since the CommandParameter is, as I understand, to provide an object?

  public ICommand SelectionChangedCommand => new Command<Friend>(SelectionChangedControl);
  public void SelectionChangedControl(Friend f)
  {
    Console.WriteLine("selection made");
  }

But the command doesn't even fire now. Clearly I am way off beam.

Any ideas please. (Oh by the way I have tried commenting out one or the other just in case).

BTW is there a reference (not MS docs) which explains this stuff in beginners terms? Is there an API reference to dot net Maui?

EDIT: From the documentation https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/collectionview/selection

Single selection When the SelectionMode property is set to Single, a single item in the CollectionView can be selected. When an item is selected, the SelectedItem property will be set to the value of the selected item. When this property changes, the SelectionChangedCommand is executed (with the value of the SelectionChangedCommandParameter being passed to the ICommand, and the SelectionChanged event fires.

How do I get at value of the SelectionChangedCommandParameter, i.e. the row object, i.e. my Friend object?

EDIT2: Somehow I think I need to get at the CurrentSelection[0] but I don't know how.

I've learnt that I can do something like this (from the docs)

SelectionChangedCommand="{Binding SelectionChangedCommand}"
SelectionChangedCommandParameter="Hello G"

and

public ICommand SelectionChangedCommand => new Command<string>( (String s) =>
{
  Console.WriteLine($"selection made {s}");
});

and the command is picked up and displayed, so my thinking is that using {Binding .} is not what I want, but what do I bind to?

SelectionChangedCommandParameter ={Binding ???}

Thanks, G.


Solution

  • first, bind SelectedItem

    SelectedItem="{Binding SelectedFriend}"
    

    then in your VM create a property for that bound item

    public Friend SelectedFriend { get; set; }
    

    then in your Command you can use that property

    public void SelectionChangedControl()
    {
      Console.WriteLine(SelectedFriend.Name);
    }