Прежде всего, убедитесь, что ваш ItemsSource="{Binding DeclinedRequest, Mode=TwoWay}"
в Listview
привязке DeclinedRequest
, ваша ViewModel также должна быть DeclinedRequest
. Затем вы использовали ListView
и добавили ViewCell
в DataTemplate
Вкладка, как в следующем коде.
<ListView
ItemsSource="{Binding DeclinedRequest, Mode=TwoWay}" RowHeight="150">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="CenterAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Text="FirstName" Grid.Column="0" Grid.Row="0"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
<Label Text="{Binding FirstName}" Grid.Column="1" Grid.Row="0"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Label Text="Phone Contect" Grid.Column="0" Grid.Row="1"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Label Text="{Binding PhoneNumber}" Grid.Column="1" Grid.Row="1"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Button Grid.Row="2" Text="Remove" BorderColor="IndianRed" BorderWidth="4"
BorderRadius="20"
HorizontalOptions="EndAndExpand" VerticalOptions="EndAndExpand"
FontSize="Large" TextColor="Black" Command="{Binding BindingContext.RemoveCommand}"
BackgroundColor="White"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
На фоне этого макета вы должны добавить BindingContext = new MyViewModel();
public MainPage()
{
InitializeComponent();
BindingContext = new MyViewModel();
}
Я использовал данные stati c для проведения теста. Вот код.
public class MyViewModel {
public ICommand RemoveCommand { protected set; get; }
public ObservableCollection<DryCleanerCustomerMapModel> DeclinedRequest
{
get =>IncomingRequest();
}
private ObservableCollection<DryCleanerCustomerMapModel> IncomingRequest()
{
var mylist=new ObservableCollection<DryCleanerCustomerMapModel>();
mylist.Add(new DryCleanerCustomerMapModel() { FirstName="test1", PhoneNumber=123456789 });
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test2", PhoneNumber = 123456789 });
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test3", PhoneNumber = 123456789 });
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test4", PhoneNumber = 123456789 });
return mylist;
}
public MyViewModel()
{
RemoveCommand= new Command<DryCleanerCustomerMapModel>((key) =>
{
DeclinedRequest.Remove(key);
});
}
//public event PropertyChangedEventHandler PropertyChanged;
//protected virtual void OnPropertyChanged(string propertyName)
//{
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
//}
}
Вот скриншот.
Если вы хотите получить функцию удаления для Button
, вы должны изменить код Buttom
xaml, как показано ниже.
<ListView x:Name="Mylist"
ItemsSource="{Binding DeclinedRequest, Mode=TwoWay}" RowHeight="150">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="CenterAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Text="FirstName" Grid.Column="0" Grid.Row="0"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
<Label Text="{Binding FirstName}" Grid.Column="1" Grid.Row="0"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Label Text="Phone Contect" Grid.Column="0" Grid.Row="1"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Label Text="{Binding PhoneNumber}" Grid.Column="1" Grid.Row="1"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Button Grid.Row="2" Text="Remove" BorderColor="IndianRed" BorderWidth="4"
BorderRadius="20"
HorizontalOptions="EndAndExpand" VerticalOptions="EndAndExpand"
FontSize="Large" TextColor="Black" Command="{Binding BindingContext.RemoveCommand, Source={x:Reference Name=Mylist}}" CommandParameter="{Binding .}"
BackgroundColor="White"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Вы должны изменить ViewModel, как показано ниже.
public class MyViewModel:INotifyPropertyChanged {
public ICommand RemoveCommand { protected set; get; }
ObservableCollection<DryCleanerCustomerMapModel> mylist;
public ObservableCollection<DryCleanerCustomerMapModel> DeclinedRequest
{
get => IncomingRequest();
set
{
if (mylist != value)
{
mylist = value;
OnPropertyChanged("DeclinedRequest");
}
}
}
// public ObservableCollection<DryCleanerCustomerMapModel> DeclinedRequest { get; set; }
private ObservableCollection<DryCleanerCustomerMapModel> IncomingRequest()
{
mylist = new ObservableCollection<DryCleanerCustomerMapModel>();
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test1", PhoneNumber = 123456789 });
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test2", PhoneNumber = 123456789 });
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test3", PhoneNumber = 123456789 });
mylist.Add(new DryCleanerCustomerMapModel() { FirstName = "test4", PhoneNumber = 123456789 });
return mylist;
}
public MyViewModel()
{
RemoveCommand = new Command<DryCleanerCustomerMapModel>( (key) =>
{
var my = (DryCleanerCustomerMapModel)key ;
mylist.Remove(my);
});
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Здесь выполняется gif.