Forum Xamarin.Forms

Binding Command in ListView ItemTapped

ganesh96ganesh96 Member ✭✭✭

Hi,

I have a button which opens the Camera using a class called CameraModel. This is done with a Binding Command.

Button to open Camera:

Part of CameraModel.cs:

Besides that, I also have a ListView with several items. I use ItemTapped, where only one of the items performs something when tapped:

Now I face the following problem as you already see in the previous picture:
I want to call CameraModel.cs from one specific ListView Item.
For example:

Text1
Text2
Text3
Text4
Text5

When ‘Text3’ is tapped, you can see that the DisplayAlert will show something, but now I want to add that when I tap ‘Text4’ the Camera will be opened (using CameraModel).

I have no idea how to add the Binding Command to one specific ListView Item, or how to change the code so that a Binding Command is not needed.

How can I do this?

Hope someone can help.

Regards, Ganesh

Tagged:

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    I have no idea how to add the Binding Command to one specific ListView Item, or how to change the code so that a Binding Command is not needed.

    If we want to remain the logic in command ,what we should do

    1. Create a button in listview.item and binding Command on it .

    2. Inside command function implement different logic depends on the index in the itemsource .

    It is a bit complex, i suggest you remove Command and move the logic into ItemTapped event .

    Xaml

        <ListView x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout >
                            <Label Text="{Binding Result}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    Code behind

    public class CameraModel
        {
            private string _result;
    
            public string Result
            {
                get
                {
                    return _result;
                }
                set
                {
                    if(_result != value)
                    {
                        _result = value;
                    }
                }
            }
        }
    
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class Page2 : ContentPage
        {
            List<CameraModel> List { get; set; }
    
            public Page2()
            {
                InitializeComponent();
    
                List = new List<CameraModel>();
    
                for(int i = 0; i < 5; i++)
                {
                    List.Add(new CameraModel {Result = ("Text"+(i+1).ToString()) });
                }
    
                listView.ItemsSource = List;
            }
    
            private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
            {
                int index = e.ItemIndex;
    
                if(index == 3)
                {
                    //show alert
                }
                else if (index == 5)
                {
                    //open camera
                }
            }
        }
    
  • ganesh96ganesh96 Member ✭✭✭

    Hi ColeX,

    Thanks for the response.

    I think I didn't explain my issue well enough. I'm sorry for that. Please look at the code example in the attachment (I made this example for Android). This is a RAR-file, because ZIP was too large.

    You can see the button for opening the camera, including asking for permissions. Now I want to remove this button and open the camera when I tap on Text3 in the ListView. I have no idea how to do that.

    I hope my issue is clear now.

    Regards, Ganesh

Sign In or Register to comment.