Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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.

like image 719
gfmoore Avatar asked Oct 12 '25 17:10

gfmoore


1 Answers

When you use . at CollectionView.SelectionChangedCommandParameter, it points at the BindingContext of its parent view.
e.g. If your CollectionView is in a ContentPage, . points at the BindingContext of the ContentPage.

If you want a reference of each item in FriendsList, one of the solutions is to use SelectedItem.
Try something like this:

<CollectionView 
    Grid.Row="2"
    x:Name="FriendsList"
    SelectionMode="Single"
    SelectionChangedCommand="{Binding SelectionChangedCommand}" 
    SelectionChangedCommandParameter="{Binding Path=SelectedItem, Source={x:Reference FriendsList}}">

or

<CollectionView 
    Grid.Row="2"
    SelectionMode="Single"
    SelectionChangedCommand="{Binding SelectionChangedCommand}" 
    SelectionChangedCommandParameter="{Binding Path=SelectedItem, Source={RelativeSource Self}}">

References:
Bind to self (Source={RelativeSource Self}}):
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/relative-bindings#bind-to-self

like image 119
flint Avatar answered Oct 15 '25 05:10

flint