I want to delete the selected Item from the ListBox. Code:

SearchPath.Items.Remove(SearchPath.Items[SearchPath.SelectedIndex]); 

or

 System.Collections.IList selectedItems = SearchPath.SelectedItems; if (selectedItems.Count != 0) { for (int i = selectedItems.Count - 1; i >= 0; i--) SearchPath.Items.Remove(selectedItems[i]); } 

It does not work because the original collection is in use.

At work, they said to remove directly from the model. How much is this correct and why, then, is the Remove method?

    1 answer 1

    Got it. Based on the MVvM pattern (Model-View-Model View), all work is done directly with the view model.

    The code is:

     if (SearchPath.SelectedItems.Count != 0) { foreach (var item in SearchPath.SelectedItems) _searchPathsList.Remove(item.ToString()); SearchPath.Items.Refresh(); } 
    • You somehow misunderstood. The code that removes items should be not a View, but a VM. UI should be updated automatically, through the binding. - VladD
    • If everything is properly attached, Refresh should not be needed. If it is needed, there is an error somewhere. - VladD
    • Similarly, I will do it through DataContext. - Sergey
    • And where is the result? Made? - MaximK