1

I have a ListView and since it supports multiple selection I have a button where I gather all the SelectedItems and pass them using CommandParameter. Being very new to this I really don't know how to do so with parameters. How do I access the list when it has been passed to my ViewModel? Please see below code:

View

<ListView x:Name="ListView"  ItemsSource="{Binding myModel.myCollection}">
<Button Command="{Binding SelectBtnOnClickCommand}" CommandParameter="{Binding SelectedItems, ElementName=ListView}">

ViewModel

public class SiteListViewModel
{
    public ICommand AddBtnOnClickCommand { get; }
    private ICommand _selectBtnOnClickCommand;
    public ICommand SelectBtnOnClickCommand
    {
        get
        {
            if (_selectBtnOnClickCommand == null)
                _selectBtnOnClickCommand = new RelayCommand(o =>
                {
                    var selectedSites = (o as IList);
                    if (selectedSites != null)
                    {
                        foreach (var model in selectedSites.OfType<SiteUrlsModel>())
                        {
                            //
                        }
                    }
                });
            return _selectBtnOnClickCommand;
        }
    }

    private readonly IWindowService _windowService;
    public static SiteUrlsModel SiteUrlsModel { get; } = new SiteUrlsModel();
    public ObservableCollection<SiteUrlsModel> SelectedSites { get; set; }
    private readonly ClientContext _clientContext = new ClientContext();


    public SiteListViewModel(IWindowService windowService)
    {
        _windowService = windowService;
        AddBtnOnClickCommand = new RelayCommand(AddBtnOnClick);
        //SelectBtnOnClickCommand = new RelayCommand(SelectBtnOnClick);
        RefreshSiteListView();
    }

    public void AddBtnOnClick()
    {
        _addSiteWindow = new AddSite(this);
        _addSiteWindow.Show();
    }

    public void SelectBtnOnClick(ObservableCollection<SiteUrlsModel> checkedList)
    {
        foreach (var site in checkedList)
        {
            site.IsChecked = true;
        }
    }    

    public void RefreshSiteListView()
    {
        var siteUrlsCollection = new ObservableCollection<SiteUrlsModel>(_clientContext.PopulateList());
        SiteUrlsModel.SiteUrlsCollection = siteUrlsCollection;
    }       
}

CommandClass

public class RelayCommand : ICommand
    {

        private readonly Action<object> _actionWithObject;

        public RelayCommand(Action<object> actionWithObject)
        {
            _actionWithObject = actionWithObject;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            if (parameter != null)
                _actionWithObject(parameter);
            else
                _actionWithObject(parameter);
        }

        public event EventHandler CanExecuteChanged;
    }
1
  • Not what you were asking, but.... Rather than using your own implementation of relaycommand, I suggest you use the one in mvvmlight. You can then find loads of examples of using that. And the commandparameter would be strongly typed. Commented Dec 13, 2018 at 16:24

1 Answer 1

1

ListView.SelectedItems is an IList:

private ICommand _selectBtnOnClickCommand;
public ICommand SelectBtnOnClickCommand
{
    get
    {
        if (_selectBtnOnClickCommand == null)
            _selectBtnOnClickCommand = new RelayCommand(o =>
            {
                var selectedSites = (o as IList);
                if (selectedSites != null)
                {
                    foreach (var model in selectedSites.OfType<SiteUrlsModel>())
                    {
                        //
                    }
                }
            });
        return _selectBtnOnClickCommand;
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks, I am more focused on getting the code to be hit and seeing it populated in debug mode though, sorry if my code does not set things properly into context but as the command is just a property it is not in the constructor like the rest of my commands. I want to know how to set it to the list that it has received. e.g. SelectBtnOnClickCommand = new RelayCommand(SelectBtnOnClick(mylist));
It gets hit but only when the class is being instantiated. After that it does not trigger a command when the button is clicked. My other commands are in my constructor but they are basic and do not handle any parameters.
When the class is being instantiated...? Are you really executing the command then? What's the DataContext of the Button? Please post your full view model code.
The DataContext of the View is the ViewModel, so i suppose to answer you its the viewmodel. ViewModel being the one I represented above, thanks.
I have done that now, I didn't because I simply wanted to show what was essential for my question, rather than show a bunch of unrelated functions, thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.