2016-07-28 109 views
0

我有一个列表视图有一个绑定到一个ObservableCollection,我想要它,这样当一个按钮被按下的项目被从列表和SQLite数据库中删除,到目前为止它只从数据库中删除,除非我重新启动应用程序,那么该项目不再在ListView中,有人可以告诉我我做错了什么?从ObservableCollection删除一个项目

代码背后:

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> 

+0

您正在从集合中正确删除项目。你可以展示你的XAML代码,因为我认为问题可能出在哪里。 – AlexDrenea

+0

添加列表视图XAML – matthewfuller

+0

该绑定是如何工作的? ItemsSource = {绑定模式= TwoWay}?您的ViewModel是您的收藏本身吗? – AlexDrenea

回答

0

我想修改的ObservableCollection你应该遍历集合的副本。

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

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

试图实现这一点,它似乎有同样的结果 – matthewfuller

+0

你试过调试,它说什么? –

+0

他没有迭代集合,他正在迭代他正在移除的项目,所以没关系。无需复制 – AlexDrenea

0

基于最新的代码示例,它看起来像你实际上并没有绑定您的ItemSource而是从代码,很可能会造成您的问题分配给它。

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

<ListView x:Name="IngredientList" ItemsSource="{Binding IngredientsCollection}" SelectionMode="Single" BorderThickness="0,1,0.5,0" BorderBrush="#FF007575" LayoutUpdated="ListUpdated"> 

TestListViewBinding方法删除IngredientList.ItemsSource = IngredientsCollection;,因为这将通过结合

处理确保您查看的DataContext被设置为分类您在哪里申报收集。因为它似乎是背后的代码,你可以将其设置在构造

public Ingredients() 
{ 
    this.InitializeComponent(); 
    this.DataContext = this; 
    IngredientsCollection = new ObservableCollection<Ingredient>() 
    TestViewBinding(); 
}; 

确保您IngredientsCollection是一个属性,而不是一个领域。

public ObservableCollection<Ingredient> IngredientsCollection { get; set; } 

因为你的页面没有实现INotifyPropertyChanged,该IngredientsCollection需要,使其被绑定到构造函数初始化。

很显然,这不是设置MVVM的理想方式,但它是启动您的开始。

+0

这些项目不再在列表视图中显示 – matthewfuller

+0

代码中缺少太多的代码段。你能分享整个XAML文件和整个代码文件吗? – AlexDrenea

+0

与您建议的更改? – matthewfuller