我有一個列表視圖有一個綁定到一個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="" 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>
您正在從集合中正確刪除項目。你可以展示你的XAML代碼,因爲我認爲問題可能出在哪裏。 – AlexDrenea
添加列表視圖XAML – matthewfuller
該綁定是如何工作的? ItemsSource = {綁定模式= TwoWay}?您的ViewModel是您的收藏本身嗎? – AlexDrenea