C# 删除可观察到的收集项
我已经创建了一个ObservableCollection并将其绑定到我的Listview。在将“我的项目”加载到ListView之前,将使用Linq对其进行排序,然后将其添加到ListView:C# 删除可观察到的收集项,c#,listview,windows-phone-8,observablecollection,C#,Listview,Windows Phone 8,Observablecollection,我已经创建了一个ObservableCollection并将其绑定到我的Listview。在将“我的项目”加载到ListView之前,将使用Linq对其进行排序,然后将其添加到ListView: //Get's the Items and sets it public ObservableCollection<ItemProperties> ItemCollection { get; private set; } //Orders the Items alphabetically
//Get's the Items and sets it
public ObservableCollection<ItemProperties> ItemCollection { get; private set; }
//Orders the Items alphabetically using the Title property
DataContext = ItemCollection.OrderBy(x => x.Title);
<!--ItemCollection has been binded to the ListView-->
<ListView ItemsSource="{Binding}"/>
这是我的ItemProperties类:
public class ItemProperties : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ItemProperties() { }
private string m_Title;
public string Title
{
get { return m_Title; }
set
{
m_Title = value;
OnPropertyChanged("Title");
}
}
private string m_Post;
public string Post
{
get { return m_Post; }
set
{
m_Post = value;
OnPropertyChanged("Post");
}
}
private string m_Modified;
public string Modified
{
get { return m_Modified; }
set
{
m_Modified = value;
OnPropertyChanged("Modified");
}
}
private string m_ID;
public string ID
{
get { return m_ID; }
set
{
m_ID = value;
OnPropertyChanged("ID");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(name));
}
}
编辑
如何加载我的项目:
public async void GetList()
{
var AppStorage = ApplicationData.Current.LocalFolder;
var noteFolders = await AppStorage.GetFolderAsync(@"folder\files\");
var Folders = await noteFolders.GetFoldersAsync();
ItemCollection = new ObservableCollection<ItemProperties>();
foreach (var noteFolder in Folders)
{
ItemCollection.Add(new ItemProperties { Title = readTitle, Post = readBody, ID = noteFolder.Name, Modified = timeFormat });
}
//code which readers and adds text to the properties...
DataContext = ItemCollection.OrderBy(x => x.Title);
}
public异步void GetList()
{
var AppStorage=ApplicationData.Current.LocalFolder;
var noteFolders=wait AppStorage.GetFolderAsync(@“folder\files\”);
var Folders=await noteFolders.GetFoldersAsync();
ItemCollection=新的ObservableCollection();
foreach(文件夹中的文件夹)
{
添加(新的ItemProperties{Title=readTitle,Post=readBody,ID=noteFolder.Name,Modified=timeFormat});
}
//读取并向属性中添加文本的代码。。。
DataContext=ItemCollection.OrderBy(x=>x.Title);
}
您可以从ListView获取所选项目并将其删除:
if (lvElement.SelectedIndex == -1) return;
ItemProperties selectedProperty = (ItemProperties)lvElement.SelectedItem;
// remove selectedProperty from original collection
我的解决办法是:
ItemCollection = new ObservableCollection<ItemProperties>(ItemCollection.OrderBy(a => a.Title));
DataContext = ItemCollection;
ItemCollection=newobserveCollection(ItemCollection.OrderBy(a=>a.Title));
DataContext=ItemCollection;
似乎我必须将ItemCollection重新初始化为一个新的ObservableCollection,并一次性对其进行排序,而不是加载项目然后进行排序。这样做是因为它有一个列表,首先添加项目,然后是另一个列表,需要排序。为了避免这种情况,我不得不一次完成所有的工作。上面帮助了我。我是从中获得的。
btn\u Delete\u单击
应该只删除集合中的选定项MenuFlyoutItem
是一个contextMenu
。当用户持有ListViewItem时,菜单使用项
显示删除
选项,当用户单击它时,它应该只删除所选的ListViewItem
。我添加了DataContext作为ItemProperties
,以获取整个ListViewItem对象,从而删除该项。希望这是有道理的。谢谢你,但我已经得到了答案。我会很快发布它。您可能想考虑使用WPF的<代码> CollectionViewSource <代码>来对项目进行排序和分组,而不是在设置<代码> DATACONTEXT/<代码>之前执行排序。我打算用它来做别的事情,而不是这个。
ItemCollection = new ObservableCollection<ItemProperties>(ItemCollection.OrderBy(a => a.Title));
DataContext = ItemCollection;