Using Windows Phone 8
So I've been playing around with ObservableCollection
and Listboxes
and binding them but I've never played around with INotifyPropertyChanged
. I hear that this will make a lot of things easier i.e. automatically detecting if the something in the ObservableCollection
changes.
If its not too much to ask is it possible for someone to provide me with a simple code sample which has these functions:
- Add
- Delete
So basically Just adding the items to the ObservableCollection and a button to delete the selected item from the ObservableCollection and this will update the ObsevableCollection.
It's just that I've never understood how the INotifyPropertyChanged
works. Online samples didn't seem to work for me and all I'm asking is a simple sample of that.
UPDATE
I've managed to add ObservableCollection.
public partial class MainPage : PhoneApplicationPage
{
public AddItems LoadItems = new AddItems();
public MainPage()
{
InitializeComponent();
listBox.DataContext = LoadItems;
}
public class Items
{
public string ItemTitle { get; set; }
public string ItemBody { get; set; }
public string FolderID { get; set; }
}
public class AddItems : ObservableCollection<Items>
{
public AddItems()
{
Add(new Items() { ItemTitle = "Book", ItemBody = "A simple Book.", FolderID = Count.ToString() });
Add(new Items() { ItemTitle = "Paper", ItemBody = "Something to write on.", FolderID = Count.ToString() });
Add(new Items() { ItemTitle = "Pen", ItemBody = "Something you use to write.", FolderID = Count.ToString() });
}
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
}
}
Now if I want to delete items from the listbox how can I do that? I tried:
LoadItems.Remove(listBox.SelectedItem);
But that didn't work. How can I delete a selected item and let the ObservableCollection automatically detect that change and do a refresh so it wont show that deleted item?
Thanks!