从WP8中的列表框中删除项目

时间:2013-11-03 17:09:26

标签: windows-phone-8 listbox windows-phone listboxitem

我是Windows手机开发的新手。我正在尝试从列表框中删除所选项目。我有数据类

public class MyDataClass 
{ 
    public string MSG { get; set; } 
    public int Id { get; set; } 
}

然后我尝试删除所选项目(Button1_Click事件)

MyDataClass item = MyDict.SelectedItem as MyDataClass; 
ObservableCollection dataList = new ObservableCollection(); 
dataList.Remove(item);

在任务中创建datalist的问题,所以对于程序的其余部分是不可用的,如何更改?

    public async Task GETFROMDB()
    {
        int a = 1;
        Database database = new Database(ApplicationData.Current.LocalFolder, "DictData.db");
        await database.OpenAsync();
        string query = "SELECT * FROM MyDICT";
        Statement statement = await database.PrepareStatementAsync(query);
        statement.EnableColumnsProperty();
        ObservableCollection<MyDataClass> dataList = new ObservableCollection<MyDataClass>();
        while (await statement.StepAsync())
        {
            rawData = string.Format(statement.Columns["value"]);
            string[] sep = new string[] { "\r\n" }; //Splittng it with new line
            string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var d in arrData)
            {
                dataList.Add(new MyDataClass() { MSG = d, Id= a });
                a++;
            }
        }
        MyDict.ItemsSource = dataList;
    }

3 个答案:

答案 0 :(得分:0)

您似乎试图从全新的集合中删除该项目 - 请尝试将其从列表框数据绑定的项目中删除。

答案 1 :(得分:0)

尝试使用按钮数据上下文

像这样

点击处理程序中的

      private void ButtonClick(object sender, RoutedEventArgs e)
      {
         Button btn = sender as Button;
         if (btn != null)
          {
             MyDataClass item = btn.DataContext as MyDataClass; 
             dataList.Remove(item);
          }

        }

答案 2 :(得分:0)

你能绑定到Task之外的dataList并使dataList静态或在Task中引用它吗?

创建列表时:

static ObservableCollection<MyDataClass> dataList = new ObservableCollection<MyDataClass>(); 
MyDict.ItemsSource = dataList;

然后在任务:

public async Task GETFROMDB()
{
    int a = 1;
    Database database = new Database(ApplicationData.Current.LocalFolder, "DictData.db");
    await database.OpenAsync();
    string query = "SELECT * FROM MyDICT";
    Statement statement = await database.PrepareStatementAsync(query);
    statement.EnableColumnsProperty();
    while (await statement.StepAsync())
    {
        rawData = string.Format(statement.Columns["value"]);
        string[] sep = new string[] { "\r\n" }; //Splittng it with new line
        string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        foreach (var d in arrData)
        {
            dataList.Add(new MyDataClass() { MSG = d, Id= a });
            a++;
        }
    }
}

然后点击:

MyDataClass item = MyDict.SelectedItem as MyDataClass; 
dataList.Remove(item);

或制作:

创建列表时:

ObservableCollection<MyDataClass> dataList = new ObservableCollection<MyDataClass>(); 
MyDict.ItemsSource = dataList;

然后在任务:

public async Task GETFROMDB(ObservableCollection<MyDataClass> dataList)
{
    int a = 1;
    Database database = new Database(ApplicationData.Current.LocalFolder, "DictData.db");
    await database.OpenAsync();
    string query = "SELECT * FROM MyDICT";
    Statement statement = await database.PrepareStatementAsync(query);
    statement.EnableColumnsProperty();
    while (await statement.StepAsync())
    {
        rawData = string.Format(statement.Columns["value"]);
        string[] sep = new string[] { "\r\n" }; //Splittng it with new line
        string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        foreach (var d in arrData)
        {
            dataList.Add(new MyDataClass() { MSG = d, Id= a });
            a++;
        }
    }
}

然后点击:

 MyDataClass item = MyDict.SelectedItem as MyDataClass; 
 dataList.Remove(item);

当然,你需要等到任务结束。