Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:11:44+00:00 2026-05-16T05:11:44+00:00

In my viewmodel, I have a list (ObservableCollection) containing items. In the view, this

  • 0

In my viewmodel, I have a list (ObservableCollection) containing items. In the view, this list is displayed in an ItemsControl. In each row, there is a “Delete” button. I want the command behind the button to remove the item from the list.

<ItemsControl ItemsSource="{Binding myList}">
    <ItemsControl.ItemTemplate>
        ...
            <Button Command="{StaticResource myDeleteCommand}" CommandParameter="???">
                Remove item
            </Button>
        ...
    </ItemsControl.ItemTemplate>
</ItemsControl>

What do I pass as the command parameter?

  • The item itself (Binding .)? Then I don’t have a reference to the list in the command, so I’d need to change my model such that each list item contains a back-reference to the list.
  • The list? Then I don’t have a reference to the item.
  • Both? Then I need to write a MultiConverter that translates the list plus the item into some custom object. Seems like a lot of overhead for such a simple task.

Any ideas? This seems like a fairly common scenario to me, so I guess there must be some well-established best-practice solution…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:11:44+00:00Added an answer on May 16, 2026 at 5:11 am

    I’ve implemented such commands in that way, that I pass the Item as Parameter. The command self knows on which list it should operate. Either trough a delegate that calls a Delete Method in my ViewModel or the command receives the list of items in it’s constructor.

    i.e. a Command with delegates

    public sealed class SimpleParameterCommandModel<T> : CommandModel
    {
        private readonly Action<T> execute;
        private readonly Func<T, bool> canExecute;
    
        public SimpleParameterCommandModel(string label, string tooltip, Action<T> execute, Func<T, bool> canExecute)
            : base(appCtx, dataCtx, label, tooltip)
        {
            if (execute == null) throw new ArgumentNullException("execute");
            this.execute = execute;
            this.canExecute = canExecute;
        }
        ...
    }
    

    usage:

    private ICommand _DeleteCommand = null;
    public ICommand DeleteCommand
    {
        get
        {
            if (_DeleteCommand == null)
            {
                _DeleteCommand = new SimpleParameterCommandModel<IEnumerable<DataObjectModel>>                      ("Delete", "Delete selection from data store", 
                    (items) => items.ToList().ForEach(i => DeleteItem(i)),
                    (items) => items != null && items.Count() > 0 && AllowDelete);
            }
            return _DeleteCommand;
        }
    }
    public void DeleteItem(DataObjectModel item)
    {
            if (item == null) { throw new ArgumentNullException("item"); }
    
        myCollection.Remove(item.Object);
    }
    

    EDIT: Forgot XAML

    <Button Command="{Binding DeleteCommand, ElementName=...}" CommandParameter="{Binding}">
            Remove item
    </Button>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a View / ViewModel where a ProductList is loaded. This list is
I have ObservableCollection<Foo> that is bound to an ItemsControl (basically displaying a list). Foo
I have ViewModel class as follows: public class ListViewModel { public ObservableCollection<InfoItem> List {
I have a view that contains a list of items, a TextBox and a
In my ViewModel I have a property, which is a list of models: private
I have a ViewModel class that contains a list of points, and I am
I have the following ViewModel public class RecommendationModel { public List<CheckBoxItem> CheckBoxList { get;
In my view model if have a: List<Car> where car has an Id and
I have a user control let say UC1 . This user control have viewmodel
I have a viewmodel which is subscribing to the event NavigationCompletedEvent. This viewmodel need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.