从uwp中删除列表中的项目

时间:2016-03-12 05:35:42

标签: c# sqlite win-universal-app

我试图在点击删除按钮时从我的列表中删除项目,但是只有单击列表才会刷新并再次单击从列表中删除项目。 这是我的代码:

private void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        var query = BreakfastList.SelectedItems;
        foreach (var item in query)
        {
            conn.Delete(item);
        }
        if (Item1.IsSelected)
        {
            List<DbManager> people = (from p in conn.Table<DbManager>()
                                      select p).OrderByDescending(q => q.id).ToList();
            BreakfastList.ItemsSource = people;
        }
        else if (Item2.IsSelected)
        {
            List<DbManager> people = conn.Table<DbManager>().OrderByDescending(q => q.id).Where(q => q.Reading == "Breakfast").ToList();
            BreakfastList.ItemsSource = people;
        }
        else if (Item3.IsSelected)
        {
            List<DbManager> people = conn.Table<DbManager>().OrderByDescending(q => q.id).Where(q => q.Reading == "Lunch").ToList();
            BreakfastList.ItemsSource = people;
        }
        else if (Item4.IsSelected)
        {
            List<DbManager> people = conn.Table<DbManager>().OrderByDescending(q => q.id).Where(q => q.Reading == "Dinner").ToList();
            BreakfastList.ItemsSource = people;
        }
    }

Item1,2,3,4是我的组合框中的项目 我列出的XAML是:

  <ListView
            ScrollViewer.HorizontalScrollMode="Auto"
            ScrollViewer.VerticalScrollMode="Enabled"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
                ScrollViewer.HorizontalScrollBarVisibility="Auto"
              Margin="0,0,0,0"
             Name="BreakfastList"
              Visibility="Visible"
                >

                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="data:DbManager"  >

                                <StackPanel Orientation="Horizontal" Width="Auto" 
                                            HorizontalAlignment="Left" VerticalAlignment="Center"
                                            Height="Auto">

                                        <StackPanel Width="50" Height="Auto" Name="GluStack" RelativePanel.AlignLeftWithPanel="True" >
                                <TextBlock Text="{Binding Glucose}" Margin="0,0,10,0" IsTextScaleFactorEnabled="False"  />
                            </StackPanel>
                                    <StackPanel Width="80" Height="Auto" Name="ReadStack" RelativePanel.RightOf="GluStack">
                                        <TextBlock Text="{Binding Reading}" Margin="0,0,10,0"  IsTextScaleFactorEnabled="False"/>
                            </StackPanel>
                                    <StackPanel Width="130" Name="Read1Stack"  Height="Auto" RelativePanel.RightOf="ReadStack">
                                        <TextBlock Text="{Binding Reading1}" Margin="0,0,10,0" IsTextScaleFactorEnabled="False" />
                            </StackPanel>
                            <StackPanel Width="Auto" Name="DateStack" RelativePanel.RightOf="Read1Stack"
                                        Height="Auto">
                                        <TextBlock Text="{Binding Date}" IsTextScaleFactorEnabled="False"  />
                            </StackPanel>

                                </StackPanel>

                        </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

1 个答案:

答案 0 :(得分:0)

嗯,我不确定,因为这不是足够的信息,但是根据你的描述第一次你的方法运行(第一次点击),BreakfastList.SelectedItems是空的;因此什么都没有删除。然后通过在其上设置ItemsSource来更新列表。设置ItemsSource后,将填充BreakfastList.SelectedItems,这就是它第二次运行的原因。

如果您设置断点:

var query = BreakfastList.SelectedItems;

你能检查一下SelectedItems是否包含元素吗?或者在下一行中,如果查询有任何?

另外,你能为早餐清单给出xaml吗?它是如何在第一次初始化的?

(抱歉,我将此作为答案,但我没有足够的分数来为您的问题撰写评论。)