![](/img/trans.png)
[英]how can i update my listview items whenever i update my list of object in uwp
[英]How can I remove items from my list in my UWP application?
我对 C# 和 XAML 都很陌生,但我需要为我的学校项目创建一个 UWP 应用程序。 我所拥有的基本上是一个简单的家具数据库。 我可以毫无问题地将项目添加到我的列表中,我也可以列出这些项目,或在列表中搜索,但是当我尝试删除一个项目时,我总是收到此错误: Collection was modified; enumeration operation may not execute.
Collection was modified; enumeration operation may not execute.
我正在使用 MVVM 技术,我认为这是问题所在,我不完全理解。 这是我所拥有的:
public class ItemManager
{
public readonly CategoryList Categories = new CategoryList();
public readonly FurnitureList Furnitures = new FurnitureList();
这仅仅是一个基本的项目经理,其中有Furnitures
列表中, FurnitureList
是ObservableCollection
。
我有 2 个 ViewModel,它们是:
public class ListItemViewModel : ObservableObjectBase
{
public Furniture Model { get; private set; }
public ListItemViewModel(Furniture model)
{
Model = model;
model.PropertyChanged += Model_PropertyChanged;
}
public ListItemViewModel()
{
}
private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == nameof(Model.FurnitureID) || e.PropertyName == nameof(Model.Description) || e.PropertyName == nameof(Model.Quantity))
{
Notify(nameof(FurnitureToShow));
Notify(nameof(FurnitureIDAndCategory));
Notify(nameof(FurnitureDescription));
Notify(nameof(FurnitureQuantityToDisplay));
}
if(e.PropertyName == nameof(Model.Category))
{
Notify(nameof(IsSensitive));
}
}
这里的FurnitureToShow
和其他只是字符串,我相信它们在这里无关紧要。 这是另一个 ViewModel(我在这个中遇到错误)
public class ListViewModel : ObservableCollection<ListItemViewModel>
{
public ListViewModel(FurnitureList model)
{
model.CollectionChanged += Model_CollectionChanged;
}
private void Model_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach (Furniture f in e.NewItems)
this.Add(new ListItemViewModel(f));
if(e.OldItems != null)
{
var toRemove = this.Items.Where(i => e.OldItems.Contains(i.Model));
foreach (var vm in toRemove) **This is the line where the error pops up**
this.Remove(vm);
}
}
}
我想要做的是,当我从 ListView 中选择一个项目时,我按下删除按钮,所选项目将从原始集合中删除。 这是 XAML 代码:
<StackPanel Orientation="Horizontal" Margin="4" Height="500" VerticalAlignment="Top">
<ListView SelectionChanged="ItemGotSelected_Filter"
SelectedItem="{x:Bind selectedItem, Mode=TwoWay}"
SelectedIndex="{x:Bind selectedIndex, Mode=TwoWay}"
x:Name="FilteredListView"
ItemTemplate="{StaticResource ContactListViewTemplate}"
SelectionMode="Single"
ShowsScrollingPlaceholders="True"
Height="500"
Width="230"
BorderThickness="1"
BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"/>
<StackPanel Width="170">
<TextBlock Margin="8,8,8,4" Style="{ThemeResource BaseTextBlockStyle}">Filter by...</TextBlock>
<TextBox x:Name="FilterByID" Width="150" Header="Furniture ID" Margin="8"
HorizontalAlignment="Left" TextChanged="OnFilterChanged"/>
<TextBox x:Name="FilterByCategory" Width="150" Header="Category" Margin="8"
HorizontalAlignment="Left" TextChanged="OnFilterChanged"/>
<Button Margin="8,20,8,4" x:Name="deleteButton" Width="80" Height="30" HorizontalAlignment="Center" Style="{StaticResource ButtonRevealStyle}" Content="Delete item" FontSize="14" Padding="1" IsEnabled="False" Click="deleteButton_Click">
</Button>
</StackPanel>
我很确定绑定的 SelectedItem 代表我想要删除的实际家具项目,但如果我错了,请纠正我。 所以最后这里是背后的 C# 代码:
public sealed partial class ThreeLineList : UserControl
{
public FurnitureList allFurnitures;
public ListViewModel AllFurnituresList { get; private set; }
public Furniture selectedItem = null;
public int selectedIndex;
private ObservableCollection<Furniture> filteredFurnitures = new ObservableCollection<Furniture>();
public ThreeLineList()
{
this.InitializeComponent();
}
public void SetModel(ItemManager itemManager)
{
this.allFurnitures = itemManager.Furnitures;
this.AllFurnituresList = new ListViewModel(allFurnitures);
this.filteredFurnitures = new ObservableCollection<Furniture>(allFurnitures);
FilteredListView.ItemsSource = filteredFurnitures;
}
private void ItemGotSelected_Filter(object sender, SelectionChangedEventArgs e)
{
deleteButton.IsEnabled = true;
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
allFurnitures.Remove(selectedItem);
//filteredFurnitures.Remove(selectedItem);
}
filteredFurnitures
和所有与过滤器有关的东西都不相关,那部分工作得很好,所以我没有包括它。 所以在我的脑海中,我的 ListView 中的项目实际上在原始列表中,因为allFurnitures
列表连接到项目管理器,如果我使用另一个视图将项目添加到列表中,它们会正确显示。 这就是我不明白为什么当我尝试从该列表中删除所选项目时会收到错误消息的原因。
枚举(例如在 foreach 循环中)是顺序的。 当您删除一个元素时,您会破坏可能导致“索引超出范围”异常的序列。 改用 for 循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.