Question

I want to bind IEnumerable collection with items in ListView. I also want to make a button that can delete a chosen element from it. Is there a possible way to make a button and when it is clicked the chosen element to be deleted from the list and from the collection?

Was it helpful?

Solution

There are a couple of ways to create a WPF application, and I'm not sure if you're using MVVM pattern or not, but this would be relatively the same across the board. Note that there are a huge number of ways of accomplishing this and this is just one of them.

Let's say that you have an ObservableCollection of type Person. You either bind it from the XAML portion or the code-behind either in your view's code-behind file or a separate ViewModel file. Either way, you end up with a control, such as ListView that has ability to tell you what item is being selected, as well as the value that is being displayed. In my ViewModel (or simple code-behind of a window), when working with the selection, I generally prefer to know what item was selected in the collection, so I'll have a property like this:

// Here we are assuming that my item collection is of type Person.
private Person _selectedItem;
public Person SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (_selectedItem == value)
        {
            return;
        }

        _selectedItem = value;
        OnPropertyChanged("SelectedItem");
    }
}

Note that you have to implement INotifyPropertyChanged in your ViewModel or code-behind to get updates on what has been changed:

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

...and bind to the SelectedItem property I showed above in your ListView (within XAML):

SelectedItem="{Binding SelectedFile}"

So, whenever I want to delete an item from my collection, I just run something like this:

private void Remove()
{
    if (SelectedItem == null)
    {
        return;
    }

    collection.Remove(SelectedItem);
}

There are many approaches, but this is how I do it. You can even go more specific and do something like this:

collection.RemoveAll(x => x.SSNNumber == SelectedItem.SSNNumber);

It's really up to you how creative and specific you get. You haven't given us much details, so I'm guessing here as far as what you need.

I would definitely not rely on index, because if you sort within XAML and not in your code-behind/ViewModel, you'll end up with a wrong index during deletion. If you choose an index route, you should sort within the code, at which point the collection with XAML would update to proper order if you have your bindings done right, and the selected item would have the proper index. However, this is a poor route, so I advise against it.

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