2016-07-28 83 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

相關問題