Working with focused items displayed in a CollectionView

EricO 21 Reputation points
2021-10-13T21:16:22.54+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-10-14T03:17:28.51+00:00

    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.


0 additional answers

Sort by: Most helpful