Я думаю, что вам нужно определить свойство ICommand и назначить с помощью Command,
Примерно так же во ViewModel.
public ICommand RequestAccepted
{
get;
set;
}
в конструкторе вы можете назначить свойство с помощью команды, подобной этой,
public CustomerTransferRequestsViewModel()
{
if (GetRequestedItems.CanExecute(null))
{
GetRequestedItems.Execute(null);
}
RequestAccepted = new Command(() =>
{
//code goes here
});
}
.XAML Page
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="RequestsPage"
xmlns:local="clr-namespace:Stack51123113"
x:Class="Stack51123113.MainPage">
<ContentPage.BindingContext>
<local:CustomerTransferRequestsViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Label
Text="لا يوجد لديك طلبات حالياً">
</Label>
<ListView
ItemsSource="{Binding RequestedItems}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label
Text="{Binding RequestUserName}">
</Label>
<Label
Text="{Binding ItemsName}">
</Label>
<Label
Text="{Binding ItemsPrice}">
</Label>
<StackLayout
Orientation="Vertical">
<Button
Text="قبول"
Command="{Binding Source={x:Reference RequestsPage}, Path=BindingContext.RequestAccepted}">
</Button>
<Button
Text="رفض">
</Button>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
ViewModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Xamarin.Forms;
namespace Stack51123113
{
public class CustomerTransferRequestsViewModel : INotifyPropertyChanged
{
public CustomerTransferRequestsViewModel()
{
if (GetRequestedItems.CanExecute(null))
{
GetRequestedItems.Execute(null);
}
RequestAccepted = new Command(() =>
{
//code goes here
});
}
ApiServices _apiServices = new ApiServices();
private ObservableCollection<GetCustomerTransferOrderRespond> _requestedItems;
private bool _emptyLableVisible;
private bool _isLoading;
public ObservableCollection<GetCustomerTransferOrderRespond> RequestedItems
{
get => _requestedItems;
set
{
if (Equals(value, _requestedItems)) return;
_requestedItems = value;
OnPropertyChanged();
}
}
public bool EmptyLableVisible
{
get => _emptyLableVisible;
set
{
if (Equals(value, _emptyLableVisible)) return;
_emptyLableVisible = value;
OnPropertyChanged();
}
}
public bool IsLoading
{
get => _isLoading; set
{
if (Equals(value, _isLoading)) return;
_isLoading = value;
OnPropertyChanged();
}
}
public ICommand GetRequestedItems
{
get
{
return new Command(async () =>
{
RequestedItems = new ObservableCollection<GetCustomerTransferOrderRespond>(new List<GetCustomerTransferOrderRespond>()
{
new GetCustomerTransferOrderRespond(),
new GetCustomerTransferOrderRespond(),
});
});
}
}
public ICommand RequestAccepted
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ApiServices
{
public ApiServices()
{
}
}
public class GetCustomerTransferOrderRespond
{
}
}