Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
Hi All,
i am trying to implement SelectAll/UnSelectAll and Multiple items selected from Listbox using Wpf MVVM.but i am getting issue in ViewModel.i written the below code.any one could you please share the solution to me.
XAML Code
<ListBox Name="lstReferenc" ItemsSource="{Binding Granularitylist}" DisplayMemberPath="Name" 
                             SelectionMode="Extended"  Margin="103,8,9,8" SelectedValuePath="Name"  >
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBox}">
                                <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                            </Style>
                        </ListBox.ItemContainerStyle></ListBox><pre>
In View Model Code is 
 public ICommand SelectAll { get; private set; }
        public ICommand UnSelectAll { get; private set; }
public ExtractInfoVM()
        {
            try
            {
                VendorList= GetVendors();
                
                SelectAll = new DelegateCommand(SelectAllmethod);
                UnSelectAll = new DelegateCommand(UnSelectAllmethod);
            }
            catch (Exception ex)
                {
                    throw ex;
                }

        }
public void SelectAllmethod()
        {
            foreach (item i in yourCollection)
            {
                i.IsSelected = true;
            }
        }
here i am getting  compile time error cause of 'item'

What I have tried:

and i tried to implement multiple items selected from but when i debugging it's display single item only for that i am using below code 
<pre>public List<string> _SelectedGranularityItems;
        public List<string> SelectedGranularityItem
        {
            get { return _SelectedGranularityItems; }
            set { _SelectedGranularityItems = value; }
        }


Any one could you please help to me.
Posted
Updated 14-Jun-17 10:20am

1 solution

Quote:
i written the below code.
foreach (item i in yourCollection)
{
    i.IsSelected = true;
}

You clearly didn't write that code. You found some pseudo-code posted somewhere *, and copied it to your project without bothering to understand what it's doing, or what parts you need to replace.

You need to replace item with the type of the items in your collection, or with the var keyword. You haven't told us the type name of the items in your collection, so you'll have to work that one out for yourself.

You need to replace yourCollection with the name of the property or field which stores your collection. At a guess, that would be either Granularitylist or VendorList - you mentioned both in your question, and it's not clear which collection you're trying to use.

Whatever type is in your collection, you'll need to make sure it has a public IsSelected boolean property. You'll also need to fix your ItemContainerStyle, which is currently targeting ListBox instead of ListBoxItem.

I suggest you go back to where you found the code, re-read the entire post, and try to understand it. Don't just randomly copy blocks of code to your project without understanding what they do!


* At a guess, you copied the code from this StackOverflow answer[^].
 
Share this answer
 
v2

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