I'm outputting data to a Listview on a page, for each item in the page I assign an 'Id' to a buttons command parameter, taken from a database (part of RModels). I'm trying to bind the button to call the command EditGSM but doesn't appear to do so. If i move the button code outside the Listview it triggers the command fine.
Content Page:
<ContentPage.BindingContext>
<vm:GSMUnitsViewModel/>
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Text="Logout" Command="{Binding LogoutCommand}" Clicked="Logout_Clicked" />
</ContentPage.ToolbarItems>
<StackLayout>
<ListView ItemsSource="{Binding RModels}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding IMEI}" />
<Label Text="{Binding Description}" />
<Label Text="{Binding Model}" />
<Label Text="{Binding Created}" />
<Button Command="{Binding EditGSM}" CommandParameter="{Binding Id}" Text="Edit GSM" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
ViewModel:
public class GSMUnitsViewModel : INotifyPropertyChanged
{
ApiServices apiServices = new ApiServices();
private List<GSMRegModelList> _rmodels;
public GSMUnitsViewModel()
{
Task.Run(async () => RModels = await apiServices.GetGsmUnits(Settings.AccessToken));
}
public List<GSMRegModelList> RModels
{
get { return _rmodels; }
set
{
_rmodels = value;
OnPropertyChanged();
}
}
public ICommand EditGSM
{
get
{
return new Command(() =>
{
var test = "dfdf";
});
}
}
public ICommand LogoutCommand
{
get
{
return new Command(() =>
{
Settings.AccessToken = "";
Settings.Username = "";
Settings.Password = "";
});
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}