Xamarin Forms I can not get SelectedItem to run the edit Code

tim 200 Reputation points
2024-04-07T14:36:19.0533333+00:00

I've tried everything I could find to get a selected row to run an edit code.

tried TapGestureRecognizer, Buttons , Selecteditem but nothing happens and the only thing that happens is the background color on the row changes when using Selecteditem.

this is my current code.

Xaml page

     <ListView x:Name="ChecklistView" ItemsSource="{Binding MyItems}" IsGroupingEnabled="True"  GroupShortNameBinding="{Binding Key}" GroupDisplayBinding="{Binding Key}" HasUnevenRows="True" SelectedItem="OnEditCommandAction" >

            <ListView.ItemTemplate >

                <DataTemplate>

                    <ViewCell>

                        <StackLayout Orientation="Horizontal">

                            <BoxView WidthRequest="1" BackgroundColor="Black" HorizontalOptions="End"/>

                            <Label Text="{Binding Sector}" 

                                HorizontalOptions="CenterAndExpand"

                                   VerticalOptions="CenterAndExpand"

                                   FontAttributes="Bold"

                                   WidthRequest="30"/>

                            <BoxView WidthRequest="1" BackgroundColor="Black" HorizontalOptions="End"/>

                            <Label Text="{Binding TreeNumber}" 
```<!-- code behind -->

namespace MapleSugar.Pages

{

```scala
[XamlCompilation(XamlCompilationOptions.Compile)]

public partial class CheckListPage : ContentPage

{

    public CheckListPage()

    {

        InitializeComponent();


    

        BindingContext = new CheckListPageModel();

    }       

}
```}

**CheckListPageModel** 

using MapleSugar.Models;

using MapleSugar.PageModels.Base;

using MapleSugar.ViewModels.Buttons;

using System.Collections.ObjectModel;

using MvvmHelpers;

using MapleSugar.Services;

using System.Linq;

using System;

using Xamarin.Essentials;

using System.IO;

using MapleSugar.Pages;

namespace MapleSugar.PageModels

{

```vba
public class CheckListPageModel : PageModelBase    {

    private int _sector;

    public int Sector

    {

        get => _sector;

        set  ...

    public ObservableCollection<Grouping<string, CheckListItem>> MyItems { get; set; } = new ObservableCollection<Grouping<string, CheckListItem>>();

    public CheckListPageModel()

    {

        WorkTreeLocationItems = new ObservableCollection<WorkTreeLocationItem>();


       

        Load_Button_Clicked = new ButtonModel("Load Data", LoadCheckListAction);

        Close_App_Clicked = new ButtonModel("Close App", CloseAppAction);
```...

/* I can not get the following to be called or execute  */

```powershell
    public void OnEditCommandAction()

    {

        WorkTreeLocationItems.Insert(0, new WorkTreeLocationItem

        {

            WorkSector = Convert.ToInt32(_sector),

            WorkTreeNumber = Convert.ToInt32(_treeNumber),

            WorkSubTreeNumber = Convert.ToInt32(_subTreeNumber),

            WorkTreeSubLetter = _treeSubLetter,

            WorkTreeLocation = _treeLocation,

            WorkCircumference = Convert.ToDouble(_circumference),

            WorkTreeCombineWith = Convert.ToDouble(_treeCombineWith),

            WorkComments = _comments,

            WorkGridX = Convert.ToDouble(_gridX),

            WorkGridY = Convert.ToDouble(_gridY),

            WorkLatSec = Convert.ToDouble(_latSec),

            WorkLonSec = Convert.ToDouble(_lonSec),

            WorkContainer = _container

        });

        var treeInfoPage = new TreeInfoPage();

        treeInfoPage.BindingContext = WorkTreeLocationItems;



        /* the following does not work but I'll post this issue later  , I know the async is missing above but want to get this code to trigger., I'm show the nav code in case some other code is need to trigger this code*/

        //await INavigationService.PushAsync(treeInfoPage);

       // await _navigationService.NavigateToAsync<TreeInfoPageModel>(treeInfoPage);

        return;

    }
```TIA 

Tim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,172 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,386 Reputation points Microsoft Vendor
    2024-04-08T05:55:41.8766667+00:00

    Hello,

    Xamarin support will end on May 1, 2024, it's recommended that you migrate your Xamarin app to .NET MAUI. Please see Xamarin official support policy .

    If you want to get the selected Object in the ViewModel.

    You can add the TapGestureRecognizer for your StackLayout in the <ViewCell> like following code.

    <ListView x:Name="ChecklistView"
              ...
                >
        <ListView.ItemTemplate >
            <DataTemplate>
             <ViewCell>
                    <StackLayout Orientation="Horizontal">
                         <StackLayout.GestureRecognizers>
                             <TapGestureRecognizer                            
                                 Command="{Binding BindingContext.OnEditCommand, Source={x:Reference ChecklistView}}"
                                 CommandParameter="{Binding .}"                                                    
                                 />
                         </StackLayout.GestureRecognizers>
    

    Then, Open your CheckListPageModel. Add ICommand interface and create a Command Object.

    If you want to get current selected Object, please add an attribute in the OnEditCommandAction method.

    
     public ICommand OnEditCommand { get; set; }
      public CheckListPageModel()
    
    
     {
          OnEditCommand = new Command(OnEditCommandAction);}
    ...
    }
    
      public void OnEditCommandAction(object SelectedObj)
    {
    
    }
    

    Best Regards,

    Leon Lu


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

    **Note: Please follow the steps

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.