Click here to Skip to main content
15,888,208 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListBox that is bound to an ObservableCollection. When I click on the item in the ListBox, I want to remove it from the ListBox, and make another UI element visible based on which ListBoxItem was selected.

At present the method removes all filterItems from the ListBox regardless of what the _selectedItem.ID is equal to. What is strange is that if I add a MessageBox after the end of the Ifs it works correctly.

What I have tried:

Here is my code so far;

private void OnAddToFilterLBSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var _selectedItem = secondListBox.SelectedItem as FilterItem;
    if (_selectedItem != null)
    {
        if (_selectedItem.ID == 1)
        {
            currentItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 2)
        {
            subbieItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 3)
        {
            suppliersItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 4)
        {
            plantHireItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 5)
        {
            architectsItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 6)
        {
            qsItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 7)
        {
            projectManagerItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 8)
        {
            structEngItem.Visibility = Visibility.Visible;
        }
        else if (_selectedItem.ID == 9)
        {
            servEngItem.Visibility = Visibility.Visible;
        }
        filterItems.Remove(_selectedItem);
    }
}
Posted
Comments
[no name] 9-Sep-16 10:13am    
Learn how to use the debugger. We can't step through code for you.
DRD94 9-Sep-16 10:22am    
If I set a breakpoint on the line filterItems.Remove(_selectedItem); and continue through, the code works exactly how I would expect it to. If I don't have a breakpoint then all the items are removed and added to the firstListBox. I've never experienced a situation before where the breakpoint in the debugger seems to make the code work correctly? The same also applies if I add a Thread.Sleep(1000) in too.
Maciej Los 9-Sep-16 14:15pm    
Seems that listbox is binded with ObservableCollection. When you remove any item from listbox, an observablecollection is modified. You have to Invoke listbox to refresh it.
Check this: How to: Create and Bind to an ObservableCollection[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900