[英]Removing an item from an ObservableCollection
我有一个与ObservableCollection绑定的列表视图,我试图拥有它,以便在按下按钮时从列表和SQLite数据库中删除该项目,到目前为止,除非我重新启动,否则它只会从数据库中删除。应用程序,则该项目不再存在于ListView中,有人可以告诉我我在做什么错吗?
背后的代码:
namespace Epicure.Views
{
public sealed partial class Ingredients : Page
{
public Ingredients()
{
this.InitializeComponent();
TestViewBinding();
this.DataContext = this;
}
public static ObservableCollection<Ingredient> IngredientsCollection = new ObservableCollection<Ingredient>();
public void TestViewBinding()
{
var db = new SQLiteConnection(new SQLitePlatformWinRT(), App.path);
var Ingredients = new List<Ingredient>();
Ingredients = db.Table<Ingredient>().ToList();
foreach (var Ingredient in Ingredients)
{
IngredientsCollection.Add(Ingredient);
}
}
private void ListUpdated(object sender, object e)
{
if (IngredientsCollection.Count == 0)
{
LonelyPanel.Visibility = Visibility.Visible;
}
if (IngredientsCollection.Count > 0)
{
LonelyPanel.Visibility = Visibility.Collapsed;
}
}
private void RemoveClicked(object sender, RoutedEventArgs e)
{
var db = new SQLiteConnection(new SQLitePlatformWinRT(), App.path);
var Ingredients = db.Table<Ingredient>().ToList();
foreach (var Ingredient in Ingredients)
{
db.Delete(Ingredient);
IngredientsCollection.Remove(Ingredient);
}
}
private void NewIngredientClicked(object sender, RoutedEventArgs e)
{
NavigationService.NavigateToPage(typeof(NewIngredient));
}
}
}
XAML:
<Page
x:Class="Epicure.Views.Ingredients"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Epicure.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<ListView x:Name="IngredientList" ItemsSource="{Binding IngredientsCollection}" SelectionMode="Single" BorderThickness="0,1,0.5,0" BorderBrush="#FF007575" LayoutUpdated="ListUpdated">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="140" HorizontalAlignment="Stretch">
<StackPanel>
<TextBlock Text="{Binding IngredientName}"/>
<AppBarButton x:Name="DeleteButton" Style="{StaticResource EpicureRibbonButton}" Width="48" Click="RemoveClicked" Icon="Delete"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackPanel x:Name="LonelyPanel" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock x:Name="Icon" TextWrapping="Wrap" TextAlignment="Center" Text="" FontFamily="Segoe MDL2 Assets" FontSize="48" Margin="0" Foreground="#FF007575"/>
<TextBlock x:Name="Text" TextWrapping="Wrap" TextAlignment="Center" Margin="0,12,0,0" Foreground="#FF007575">
<Run Text="It's lonely here,"/>
<LineBreak/>
<Run Text="want to add something?"/>
</TextBlock>
<AppBarButton x:Name="AddIngredient" HorizontalAlignment="Stretch" Label="Add Ingredient" VerticalAlignment="Stretch" Style="{StaticResource AddButton}" Width="Auto" Margin="0,12,0,0" Foreground="#FF007575" Click="NewIngredientClicked">
<AppBarButton.Icon>
<FontIcon Glyph="" FontSize="20"/>
</AppBarButton.Icon>
</AppBarButton>
</StackPanel>
</Grid>
我认为要修改observablecollection,您应该遍历集合的副本。
尝试这样,您应该比较一个属性,最好是id。
var copy = new ObservableCollection<Ingredient>(IngredientsCollection);
copy.Remove(copy.Where(i => i.Id == Ingredient.Id).Single());
根据最新的代码示例,您似乎实际上并没有绑定ItemSource,而是从代码中分配了它,这很可能会引起您的问题。
首先更新您的XAML以将ItemsSource正确绑定到ObservableCollection
<ListView x:Name="IngredientList" ItemsSource="{Binding IngredientsCollection}" SelectionMode="Single" BorderThickness="0,1,0.5,0" BorderBrush="#FF007575" LayoutUpdated="ListUpdated">
删除IngredientList.ItemsSource = IngredientsCollection;
从您的TestListViewBinding
方法,因为这将由绑定处理
确保将View的DataContext
设置为声明Collection的类。 由于它似乎是您背后的代码,因此您只需在构造函数中进行设置
public Ingredients()
{
this.InitializeComponent();
this.DataContext = this;
IngredientsCollection = new ObservableCollection<Ingredient>()
TestViewBinding();
};
确保您的IngredientsCollection
是属性而不是字段。
public ObservableCollection<Ingredient> IngredientsCollection { get; set; }
由于您的Page未实现INotifyPropertyChanged
,因此需要在构造函数中初始化IngredientsCollection
以便将其绑定到。
显然,这不是设置MVVM的理想方法,但这是让您步入正轨的开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.