Xamarin Forms Command not being activated

685 views c#

I am having an issue with my Commmand inside of a ViewCell in a ListView. My Data Template for the List view is currently using Bindings to get multiple object data from a JSON string and I have a feeling that this is preventing my Command Binding from being accessed in my XAML here

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                <Label Text="News Articles"
                    HorizontalOptions="CenterAndExpand" />
                <ListView x:Name="GetListView" HasUnevenRows="true">
                                <StackLayout VerticalOptions="FillAndExpand" Margin="0,20,0,20">
                                            Command="{Binding ArtCommand}"
                                    <Label Text="{Binding strArticleTitle}"/>
                                    <Image Source="{Binding strArticlePhotoUrl}"/>


In my code behind I set the binding context to the view model here as well as retrieve the data for my list view.

    public partial class HomePage : ContentPage

        HomeVM viewModel;

        public HomePage()

            viewModel = new HomeVM();
            BindingContext = viewModel;

        protected async override void OnAppearing()

            HttpClient client = new HttpClient();
            string url = "https://example.net/api/Articles/GetArticles";
            var response = await client.GetAsync(url);
            if (response.IsSuccessStatusCode)
                string res = "";
                using (HttpContent content = response.Content)
                    Task<string> result = content.ReadAsStringAsync();
                    res = result.Result;
                    var ArticlesList = Articles.ArticlesItems.FromJson(res);
                    GetListView.ItemsSource = ArticlesList;
                await DisplayAlert("Connection Error", "Please Connect to the internet and try again", "Ok");


Is there any way to get around this, I'm new to Xamarin Forms.



answered question

1 Answer


The DataTemplate context of the list view is bound to the source item, This is where it expects the command.

However you can do something like this if you want you Command in the main view model for the page.


And your DataTemplate

     Command="{Binding Path=BindingContext.ArtCommand, Source={x:Reference This}}

However you'll have to set the CommandParameter="{Binding}" to get the item

posted this

Have an answer?


Please login first before posting an answer.