0

我试图绑定一个命令以从分组的 ListView 中删除一个项目变得疯狂。我的 ViewModel 继承自 BaseViewModel 以简化更改属性的通知。我通过使用两个 ObservableCollections 遵循了似乎是正确的常用方法。

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

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

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

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

这些是我的视图模型:

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>

我的基本视图模型:

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

谢谢你的时间!

4

1 回答 1

1

根据您的命令中的绑定表达式

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

RemoveCommand应该在 ViewModel OrderOTIVM中的CarrelliGrouped属性上找到。

但是CarrelliGrouped是 ObservableCollection,而不是GroupedArticlesVM

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

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

您的 XAML 将如下所示:

Command="{Binding Path=BindingContext.RemoveCommand, Source={x:Reference Name=carrelliListView}}"
于 2020-08-18T13:39:38.460 回答