1

I have a program with a class called MyClass and Location. MyClass contains an ObservableCollection of Location items and Location contains a string property called Name. In MainPage.xaml I have a LongListSelector (with a ContextMenu for each item) populated with grids representing a Location.

When I click the 'remove' menu item from the context control, it will usually remove the underlying Location object and update the view. After a few cycles of populating the LongListSelector and removing all its items, some new items that are added can't be removed anymore.

Here's an example of what I mean: The LLS originally contains 2 items. Then I delete those 2 items and add 3 more. However, I can only remove the third one, in this case, but not the first 2.

Here's the ContextMenu MenuItem click event from MainPage.xaml.cs:

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    var selectedItem = (sender as MenuItem).DataContext as Location;

    for (int i = 0; i < MyClass.Locations.Count; i++)
    {
        if (MyClass.Locations[i].Name == selectedItem.Name)
        {
            MyClass.Locations.Remove(MyClass.Locations[i]);
            break;
        }
    }
}

Prior to using a for loop, I used this LINQ code and still had the same problem:

var toRemove = MyClass.Locations.Where(x => x.Name == selectedItem.Name).SingleOrDefault();
MyClass.Locations.Remove(toRemove);

Any suggestions to fix this problem?

pcnThird
  • 2,362
  • 2
  • 19
  • 33
  • I assume the DataContext is actually an Item from the Locations? In that case you should be able to remove the Items from the Collection with something like `MyClass.Locations.Remove(selectedItem)` instead of the for loop. – ChrisK Nov 16 '13 at 22:15
  • I have tried that, but I still had the same problem, unfortunately. – pcnThird Nov 16 '13 at 22:17
  • Can you elaborate the `can't be removed anymore` a bit better? Are you still able to open the context menu? Have you tried to set a breakpoint at the condition to see if the Name's aren't matching for whatever reason? – ChrisK Nov 16 '13 at 22:20
  • The ContextMenu still shows up. I set a breakpoint and it turns out `selectedItem` is null. – pcnThird Nov 16 '13 at 22:27
  • `var selectedItem = (sender as MenuItem).DataContext as Location;` will be null if either sender is not A Menu Item (but you should get a NullReferenceException in that case) or DataContext is not a Location. What is the DataContext for an Item where the selectedItem is null? – ChrisK Nov 16 '13 at 22:28
  • My bad, `selectedItem` is not null. It has the same name as the item that was deleted before the new item was added. – pcnThird Nov 16 '13 at 22:30
  • 1
    @pcnThird - can you try to replace LLS with ListBox and check if you have the same problems? – Romasz Nov 16 '13 at 22:52
  • Wow, I'm actually surprised using a ListBox worked! Thanks, Romasz. Feel free to post it as an answer. – pcnThird Nov 16 '13 at 22:59

1 Answers1

1

I suggest you to use a ListBox instead of LLS - if you are not using grouping option. It works much better and causes less problems.
By the way I've also encountered some problems with this Control - maybe similar to yours.
Weird is also that LLS.UpdateLayout() doesn't work while in ListBox works perfect.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154