0

I am trying to get the clicked item on my collection view (what a noble idea).

I am happy with the inner workings of my view:

<CollectionView SelectionChangedCommand="{Binding ClickCommand}" SelectionMode="Single"  SelectionChangedCommandParameter="{Binding SelectedItem}" VerticalOptions="FillAndExpand" ItemsSource="{Binding WifiHotSpots}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid Padding="10">
                <Label Text="{Binding .}" FontAttributes="Bold" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

I am also happy with having the click being registered inside my command:

[RelayCommand]
public void Click(object obj)
{

}

... however, I am unhappy with my obj not containing anything. It is null.
How can I return the clicked item?

EDIT: The selected Item (which was just a test I believe):

    private object _selectedItem;
    public object SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (_selectedItem != value)
            {
                _selectedItem = value;
                OnPropertyChanged(nameof(SelectedItem));
            }
        }
    }
2
  • 1
    where are you setting SelectedItem? Commented May 13, 2024 at 13:21
  • Please show where SelectedItem is defined and how you set it. Commented May 13, 2024 at 14:17

2 Answers 2

1

I solved it myself eventually doing this:

                        <Grid.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding BindingContext.ClickCommand, Source={x:Reference WifiPageViewPage}}" CommandParameter="{Binding .}"/>
                        </Grid.GestureRecognizers>

it takes an x:name on the page for the reference,.

Sign up to request clarification or add additional context in comments.

1 Comment

Close enough...
0

I am answering this:

I am trying to get the clicked item on my collection view

First, specifying your Model class in your DataTemplate is mandatory:

<DataTemplate x:DataType="model:MyModel">

(Julian pointed out, that it is not mandatory, if you never specified your ViewModel, never skipped it myself, because I do not get binding typo warnings, TODO: test)

Second, I advise you to use tap gesture, let's say I have Grid in the DataTemplate:

<Grid.GestureRecognizers>
    <TapGestureRecognizer...

Third, the way you go get to the ViewModel, and execute the command is with:

<TapGestureRecognizer CommandParameter="{Binding .}"
                      Command="{Binding Source={RelativeSource AncestorType= 
{x:Type viewmodel:MyViewModel}}, Path=MyCommand}"/>

It is the most basic way to get the clicked item.

Edit

When you see "model:MyModel" and "local:MainPage", "viewmodel:MyViewModel", you have to understand that MyModel, MainPage, MyViewModel are classes. And model, local, viewmodel are name spaces.

And let's say we have MainViewModel, that is at the top-level namespace of my application.

To use it for binding, you do this:

xmlns:local="clr-namespace:MyApp"

And after that you set your view model as DataType:

x:DataType="local:MyViewModel"

The same way you can add your model namespace as XML namespace and use it when setting DataType of the collection. And also the same way, you can specify your view model as type for the command binding. By declaring "viewmodel" name space.

You can pick any words, not just "model", "viewmodel" etc. It is just an example.

13 Comments

Actually, specifying the x:DataType on the DataTemplate is only mandatory, when you also have specified the x:DataType anywhere higher up in the hierarchy of the ContentPage/ContentView up to where the BindingContext is set.
@Julian and what is he doing with "{Binding .}" ? I mean, he is doing some binding. There is set DataType, its not part of the code sample...
If the x:DataType is not specified on the root element or lower down in the hierarchy, the "{Binding .}" will still work. This is only a problem with compiled bindings.
@Julian I do not see everything, but leaving DataTemplates empty, without specifying the DataType is the main cause of empty CollectionViews in Release mode.
But under which conditions? Normally, that only happens when the type cannot be inferred and that's usually the case when there's a mismatch between the compiled binding and the actual type. Now, I'm always in favor of specifying x:DataType wherever possible, because it not only speed up the binding resolution, but it also makes it easier to read and navigate the code and assists tools and IDEs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.