Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单ListView删除项时保留旧值_C#_Xaml_Listview_Data Binding_Xamarin.forms - Fatal编程技术网

C# 表单ListView删除项时保留旧值

C# 表单ListView删除项时保留旧值,c#,xaml,listview,data-binding,xamarin.forms,C#,Xaml,Listview,Data Binding,Xamarin.forms,我有一个ListView,并通过数据绑定到ViewModel中的Lists属性来填充它。此外,我还有一个ListView菜单,带有一个Delete命令,也绑定到我的ViewModel。 我现在的问题是,如果我初始化了ListView,我可以删除其中的列表。如果我添加新列表,我可以删除所有列表。但是,如果我添加了新项目,我就不能删除它们,因为我从delete命令得到的列表是旧的、已经删除的列表 所以,在删除列表之后,它们似乎在某种程度上仍然存在,并且我只能删除新的列表,如果当前列表的总量高于以前删

我有一个ListView,并通过数据绑定到ViewModel中的Lists属性来填充它。此外,我还有一个ListView菜单,带有一个Delete命令,也绑定到我的ViewModel。 我现在的问题是,如果我初始化了ListView,我可以删除其中的列表。如果我添加新列表,我可以删除所有列表。但是,如果我添加了新项目,我就不能删除它们,因为我从delete命令得到的列表是旧的、已经删除的列表

所以,在删除列表之后,它们似乎在某种程度上仍然存在,并且我只能删除新的列表,如果当前列表的总量高于以前删除的列表的总量

我希望这是对我的问题的一个可以理解的解释

绑定正在工作,my ViewModel中的List属性保存正确的值,但DeleteListCommand中的“sender”ItemList是旧的ItemList

以下是我的ListView的XAML:

<ListView x:Name="listView" ItemsSource="{Binding Lists}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell x:Name="viewCell">
                <ViewCell.ContextActions>
                    <MenuItem Command="{Binding BindingContext.RenameListCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}" Text="Rename" />
                    <MenuItem Command="{Binding BindingContext.DeleteListCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}" IsDestructive="True" Text="Delete" />
                </ViewCell.ContextActions>
                <ContentView Margin="0,2,0,2"
                                    HeightRequest="50"
                                    BackgroundColor="{Binding Color}">

                    <ContentView.GestureRecognizers>
                        <TapGestureRecognizer BindingContext="{Binding Source={x:Reference listView}, Path=BindingContext}"
                                                        Command="{Binding ListTappedCommand}" 
                                                        CommandParameter="{Binding Source={x:Reference viewCell}, Path=BindingContext}" />
                    </ContentView.GestureRecognizers>
                    <ContentView.Content>

                        <Label Text="{Binding Name}"
                                    HorizontalTextAlignment="Center"
                                    VerticalTextAlignment="Center"
                                    TextColor="White" 
                                    IsEnabled="True"/>
                    </ContentView.Content>

                </ContentView>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这是我的ViewModel:

...
public ObservableCollection<ItemList> lists = new ObservableCollection<ItemList>();
        public ObservableCollection<ItemList> Lists
        {
            get { return lists; }
            set
            {
                lists = value;
                OnPropertyChanged("Lists");
            }
        }
public event PropertyChangedEventHandler PropertyChanged;

...

this.DeleteListCommand = new Command<ItemList>((sender) =>
            {
                    OnDeleteList(sender);
            });

...

public ICommand DeleteListCommand { get; set; }
private void OnDeleteList(ItemList itemList)
        {
            Lists.Remove(itemList);
        }

...

protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
。。。
公共ObservableCollection列表=新ObservableCollection();
公共可观测收集列表
{
获取{返回列表;}
设置
{
列表=值;
房地产变更(“清单”);
}
}
公共事件属性更改事件处理程序属性更改;
...
this.DeleteListCommand=新命令((发送方)=>
{
删除列表(发送方);
});
...
公共ICommand DeleteListCommand{get;set;}
私有void OnDeleteList(ItemList ItemList)
{
删除(itemList);
}
...
受保护的无效OnPropertyChanged(字符串propertyName)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}

基于示例项目,这里是您需要做的:将您的单元格命名为“viewCell”,因为您需要将单元格传递给您的模型,以便能够重置ContextAction。 更改菜单项上的绑定以传递单元格而不是ItemList。然后在模型中重置上下文操作,并从单元格的绑定上下文中获取一个项

XAML:


型号:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using TobyList_XamarinForms.Models;
using Xamarin.Forms;
using System.Linq;

namespace TobyList_XamarinForms.ViewModels
{
    public class MasterPageViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<ItemList> lists = new ObservableCollection<ItemList>();
        public ObservableCollection<ItemList> Lists
        {
            get { return lists; }
            set
            {
                lists = value;
                OnPropertyChanged("Lists");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public MasterPageViewModel()
        {
            this.AddListCommand = new Command(() =>
            {
                OnAddList();
            });
            //this.DeleteListCommand = new Command<ItemList>((sender) =>
            //{
            //  OnDeleteList(sender);
            //});

            this.DeleteListCommand = new Command<ViewCell>((sender) =>
            {
                OnDeleteList(sender);
            });

        }

        public ICommand AddListCommand { get; protected set; }
        private void OnAddList()
        {
            ItemList itemList = new ItemList() { Id = Guid.NewGuid().ToString().ToUpper(), Name = "Lorem Ipsum", Color = "#000000" };
            Lists.Add(itemList);
        }

        public ICommand DeleteListCommand { get; set; }
        //public void OnDeleteList(ItemList itemList)
        //      {
        //          Lists.Remove(itemList);
        //      }
        public void OnDeleteList(ViewCell viewCell)
        {
            viewCell.ContextActions.Clear();
            Lists.Remove((ItemList)viewCell.BindingContext);
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows.Input;
使用TobyListxamarinforms.Models;
使用Xamarin.Forms;
使用System.Linq;
命名空间tobyListxamarinForms.ViewModels
{
公共类MasterPageViewModel:INotifyPropertyChanged
{
公共ObservableCollection列表=新ObservableCollection();
公共可观测收集列表
{
获取{返回列表;}
设置
{
列表=值;
房地产变更(“清单”);
}
}
公共事件属性更改事件处理程序属性更改;
公共主页视图模型()
{
this.AddListCommand=新命令(()=>
{
OnAddList();
});
//this.DeleteListCommand=新命令((发送方)=>
//{
//删除列表(发送方);
//});
this.DeleteListCommand=新命令((发送方)=>
{
删除列表(发送方);
});
}
公共ICommand AddListCommand{get;protected set;}
私有void-OnAddList()
{
ItemList ItemList=new ItemList(){Id=Guid.NewGuid().ToString().ToUpper(),Name=“Lorem Ipsum”,Color=“#000000”};
列表。添加(项目列表);
}
公共ICommand DeleteListCommand{get;set;}
//公共无效OnDeleteList(ItemList ItemList)
//      {
//删除(itemList);
//      }
公共无效OnDeleteList(ViewCell ViewCell)
{
viewCell.ContextActions.Clear();
Lists.Remove((ItemList)viewCell.BindingContext);
}
受保护的无效OnPropertyChanged(字符串propertyName)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

什么是“项目列表”?它也是可观察的吗?它只是一个普通类,用作我的数据库模型,用于sqlite net pcl,具有字符串和bool属性。我还将这个类用于ObservableCollection,这样我就不必强制转换为不同的类型。我应该如何使它可见?那么,它不是一个列表?我知道你在使用嵌套列表。对吗?我想这会回答你的问题。不幸的是,我无法将其作为答案发布,因为我无法对此进行评分,并且bugzilla上已经报告了id为42516的错误:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using TobyList_XamarinForms.Models;
using Xamarin.Forms;
using System.Linq;

namespace TobyList_XamarinForms.ViewModels
{
    public class MasterPageViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<ItemList> lists = new ObservableCollection<ItemList>();
        public ObservableCollection<ItemList> Lists
        {
            get { return lists; }
            set
            {
                lists = value;
                OnPropertyChanged("Lists");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public MasterPageViewModel()
        {
            this.AddListCommand = new Command(() =>
            {
                OnAddList();
            });
            //this.DeleteListCommand = new Command<ItemList>((sender) =>
            //{
            //  OnDeleteList(sender);
            //});

            this.DeleteListCommand = new Command<ViewCell>((sender) =>
            {
                OnDeleteList(sender);
            });

        }

        public ICommand AddListCommand { get; protected set; }
        private void OnAddList()
        {
            ItemList itemList = new ItemList() { Id = Guid.NewGuid().ToString().ToUpper(), Name = "Lorem Ipsum", Color = "#000000" };
            Lists.Add(itemList);
        }

        public ICommand DeleteListCommand { get; set; }
        //public void OnDeleteList(ItemList itemList)
        //      {
        //          Lists.Remove(itemList);
        //      }
        public void OnDeleteList(ViewCell viewCell)
        {
            viewCell.ContextActions.Clear();
            Lists.Remove((ItemList)viewCell.BindingContext);
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}