question

EricO-1303 avatar image
0 Votes"
EricO-1303 asked JarvanZhang-MSFT edited

Working with focused items displayed in a CollectionView

I have a Xamarin project with a form which displays object instances stored in an ObservableCollection. The ObservableCollection items are displayed in a CollectionView. Each item contains its own Picker and Entry controls. My problem is, I need to identify which item the user is working with. If the user touches an area which does not contain controls, in order to select a new item, the CollectionView.SelectionChanged event successfully fires. However, if an item is focused and the user touches one of the controls in another item, the SelectionChanged event does not fire and I'm unable to identify which item the user is targeting. I need to be able to work with other properties in the object of a focused item. Maybe there is something simple which I'm overlooking?

Thanks

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

However, if an item is focused and the user touches one of the controls in another item, the SelectionChanged event does not fire and I'm unable to identify which item the user is targeting.

The priority of the control's event is higher than SelectionChanged event. To work with other properties, you could get the BindingContext of the currentItem to handle the data. Modify the value of the model object to update the item view.

Here is the sample code, you could refer to it.

<DataTemplate>
    <StackLayout>
        <Label Text="{Binding TheContent}"/>
        <Entry Placeholder="enter the word" TextChanged="Entry_TextChanged"/>
    </StackLayout>
</DataTemplate>

Entry's TextChanged event:

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
    var entry = sender as Entry;
    var model = entry.BindingContext as _Model;
    //change the value of the model to update the item view

    //for example:
    model.TheContent = e.NewTextValue;
}


Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BindingContext is the piece I missed.

Thank you.

0 Votes 0 ·