Xamarin:Grouped ListView未使用MVVM绑定delete命令



我正疯狂地试图绑定一个命令,以便从分组的ListView中删除一个项。我的ViewModels继承自BaseViewModel,以便于通知更改的属性。我使用了两个ObservableCollections,遵循了似乎是正确的常见方法。

这些是我的ViewModel,显示正确,但我无法将RemoveCommand绑定到每个项(类型为ArticleForOrdineOTIVM(。我使用构造函数来创建虚拟对象。我在命令="中的xaml文件中尝试了各种组合&";,例如,通过将Source引用指向列表视图名称,但我无法使其工作。它总是说我无法到达内部上下文的范围,但最多我可以在OrderOTIVM中绑定一个命令,而不是进一步绑定(例如命令"TestCommand"(。我需要这样做,因为我想删除、编辑和做列表中每个项目的内容,之前解释的错误是:

绑定:在"System.Collections.ObjectModel.ObsObservableCollection `1[BrScanner.ViewModels.GroupedArticlesVM]"上找不到"RemoveCommand"属性,目标属性:"Xamarin.Forms.Button.Command">

在View文件中,我将BindingContext设置为:

public partial class OrderOTIView : ContentPage
{
public OrderOTIView()
{
InitializeComponent();
OrderOTIVM orderOTIViewModel = new OrderOTIVM();
BindingContext = orderOTIViewModel;
}
}

这些是我的ViewModels:

public class OrderOTIVM : BaseViewModel
{
ObservableCollection<GroupedArticlesVM> _carrelliGrouped;
public ObservableCollection<GroupedArticlesVM> CarrelliGrouped { get => _carrelliGrouped; set {  _carrelliGrouped = value; OnPropertyChanged();}
}
public OrderOTIVM()
{
CarrelliGrouped = new ObservableCollection<GroupedArticlesVM>();
GroupedArticlesVM car1 = new GroupedArticlesVM();
car1.Carrello.Nome = "Carrello A";
CarrelliGrouped.Add(car1);
GroupedArticlesVM car2 = new GroupedArticlesVM();
car2.Carrello.Nome = "Carrello B";
CarrelliGrouped.Add(car2);
}
public Command TestCommand
{
get
{
return new Command(
(x) => {
Debug.WriteLine("TestCommand");
});
}
}
}
public class GroupedArticlesVM :  ObservableCollection<ArticleForOrdineOTIVM>
{
CarrelloMinimarketVM _carrello;
public CarrelloMinimarketVM Carrello { get => _carrello; set { _carrello = value; } }
public GroupedArticlesVM()
{
Items.Add(new ArticleForOrdineOTIVM());
Items.Add(new ArticleForOrdineOTIVM());
Items.Add(new ArticleForOrdineOTIVM());
Items.Add(new ArticleForOrdineOTIVM());
Carrello = new CarrelloMinimarketVM();
}

public Command<ArticleForOrdineOTIVM> RemoveCommand
{
get
{
return new Command<ArticleForOrdineOTIVM>(
(articolo)=>{
Items.Remove(articolo);
});
}
}
}
public class CarrelloMinimarketVM : BaseViewModel
{
string _nome;
public CarrelloMinimarketVM()
{
this.Nome = "CARRELLO";
}
public string Nome { get => _nome; set { _nome = value; OnPropertyChanged(); } }
}
public class ArticleForOrdineOTIVM : BaseViewModel
{
string _oarti;
string _tarti;
int _amount;
public string Oarti { get => _oarti;    set {   _oarti = value;     OnPropertyChanged(); } }
public string Tarti { get => _tarti;    set {   _tarti = value;     OnPropertyChanged(); } }
public int Amount { get => _amount;     set {   _amount = value;    OnPropertyChanged(); } }
public ArticleForOrdineOTIVM()
{
this.Oarti = "Oarti blabla";
this.Tarti = "Descrizione blabla";
this.Amount = 22;
}  
}

这是我的Xaml代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="BrScanner.Views.OrderOTIView"
x:Name="OrderOTIPage"
>
<ListView x:Name="carrelliListView" ItemsSource="{Binding CarrelliGrouped}"
HasUnevenRows="True"
GroupDisplayBinding="{Binding Carrello.Nome}"
IsGroupingEnabled="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="10">
<Label Text="{Binding Oarti}"/>
<Label Text="{Binding Tarti}"/>
<Button Text="cancella" 
Command="{Binding Path=BindingContext.CarrelliGrouped.RemoveCommand, Source={x:Reference Name=OrderOTIPage}}"
CommandParameter="{Binding .}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>

我的BaseViewModel:

public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

谢谢你抽出时间!

根据命令中的绑定表达式

Command="{Binding Path=BindingContext.CarrelliGrouped.RemoveCommand, Source={x:Reference Name=OrderOTIPage}}"

RemoveCommand应在ViewModelOrderOTIVM中的属性CarrelliGrouped上找到。

但是CarrelliGrouped是一个ObservableCollection,而不是GroupedArticlesVM

因此,在ObservableCollection上找不到RemoveCommand错误。

要解决您的问题,您应该在OrderOTIVM中移动RemoveCommand。在该命令中,您应该添加一些逻辑以在列表中查找,以查找要删除的项。

您的XAML将如下所示:

Command="{Binding Path=BindingContext.RemoveCommand, Source={x:Reference Name=carrelliListView}}"

最新更新