从ObservableCollection中删除项目

时间:2016-07-28 17:13:49

标签: c# sqlite uwp 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="&#xED56;" 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="&#xEC09;" FontSize="20"/>
            </AppBarButton.Icon>
        </AppBarButton>
    </StackPanel>
</Grid>

2 个答案:

答案 0 :(得分:0)

我认为要修改observablecollection,你应该遍历集合的副本。

尝试这样做,你应该比较一个属性,最好是id。

var copy = new ObservableCollection<Ingredient>(IngredientsCollection);
copy.Remove(copy.Where(i => i.Id == Ingredient.Id).Single());

答案 1 :(得分:0)

基于最新的代码示例,您看起来并不是实际绑定ItemSource,而是从代码中分配它可能会导致您的问题。

首先更新您的XAML以正确地将ItemsSource绑定到ObservableCollection

IngredientList.ItemsSource = IngredientsCollection;

TestListViewBinding方法移除DataContext,因为这将由绑定

处理

确保您的View public Ingredients() { this.InitializeComponent(); this.DataContext = this; IngredientsCollection = new ObservableCollection<Ingredient>() TestViewBinding(); }; 设置为您声明Collection的类。由于它似乎是你的代码,你可以在构造函数

中设置它
IngredientsCollection

确保您的public ObservableCollection<Ingredient> IngredientsCollection { get; set; } 是属性,而不是字段。

INotifyPropertyChanged

因为您的页面没有实现IngredientsCollection,所以需要在构造函数中初始化{{1}}以便绑定它。

显然,这不是设置MVVM的理想方式,但它是让你前进的开始。

相关问题