更新时间:2023-02-13 21:20:31
lstNews.Items是页面上显示的对象列表.因此,此lstNews.Items是您的数据模板的集合,因此这就是为什么当您尝试lstNews.Items.Remove(lstNews.SelectedItem.ToString())
时失败了.
lstNews.Items is list of object that are displayed on the page. So this lstNews.Items is collection of your datatemplate so that is why when you tried lstNews.Items.Remove(lstNews.SelectedItem.ToString())
than this fails.
您应该使用lstNews.Items.Remove(lstNews.SelectedItem)
删除项目.
you should use lstNews.Items.Remove(lstNews.SelectedItem)
to delete item.
但是为了***实践,***从源中而不是从列表中删除项目.即您应该从fulllist
中删除项目,然后将其重新分配为lstNews.ItemsSource = fulllist;
But for best practice it is prefered to delete item from the source not from the list. i.e. You should delete item from fulllist
and reassign it as lstNews.ItemsSource = fulllist;
您的代码中所做的更改
fulllist
应该是 ObservableCollection ,以便对数据所做的所有更改都可以反映到UI.
将List转换为ObservableCollection可以使用以下代码:
fulllist
should be a type of ObservableCollection so that all the changes done on data can be reflected to UI.
To convert List to ObservableCollection Following code can be used:
fulllist = new ObservableCollection<NewsData>(new nList());
添加用于从fulllist
删除数据的实现,可能的实现可能是:
Add the implementation for deleting data from fulllist
a possible implementation could be:
object obj = lstNews.SelectedItem;
if(obj is NewsData){
fulllist.Remove((NewsData)obj);
lstNews.ItemsSource = fulllist;
}