我为了绑定一个Command以便从一个已分组的ListView中删除一个项目而疯狂。 我的ViewModels继承自BaseViewModel,以简化属性更改的通知。 我通过使用两个ObservableCollections遵循了似乎是正确的通用方法。
这些是我的ViewModel,可以正确显示,但是我无法将RemoveCommand绑定到每个项目(ArticleForOrdineOTIVM类型)。我使用了构造函数来创建虚拟对象。 我在Command =“”内的xaml文件中尝试了各种组合,例如通过将Source reference指向listview名称,但是无法使其工作。它总是说我无法到达内部Context的范围,但是至多我只能在OrderOTIVM内绑定一个命令,而不能绑定其他命令(例如,命令“ TestCommand”)。我需要这样做是因为我想删除,编辑和处理列表中的每个项目,并且之前解释的错误说:
绑定:在'System.Collections.ObjectModel.ObservableCollection`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));
}
}
感谢您的时间!