Question

I'm new to windows phone development. I'm trying to delete selected item from the list box. I've got dataclass

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

Then I try to delete the selected item (Button1_Click event)

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

The problem in creating the datalist in task, so it's no availble for the rest of the program, how to change this?

    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;
    }
Was it helpful?

Solution

Can you make binding to a dataList outside the Task and make dataList static or reference to it in a Task?

When creating a list:

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

Then in Task:

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++;
        }
    }
}

Then in Click:

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

Or make it:

When creating a list:

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

Then in Task:

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++;
        }
    }
}

Then in Click:

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

Of course you need to wait until the Task is finished.

OTHER TIPS

you appear to be trying to remove the item from a brand new collection - try instead to remove it from the one that your listbox is data bound to.

Try using the button data context

like this

in your click handler

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

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top