Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an object in the runtime in my viewmodel class. This obj contains SelectedItemCollection from the listbox.

The problem is I can get the count in the run time while debugging.

SQL
((SelectedItemCollection)obj).Count

object Details, obj Count = 1   object {System.Windows.Controls.SelectedItemCollection}


but when I declared the same in the compile time I am getting the below error.
Error 9 'System.Windows.Controls.SelectedItemCollection' is inaccessible due to its protection level D:\Projects\GMS\GMSDesktop\Src\Modules\xxx.Security\ViewModels\xxx.cs 254 52

Guys, Let me know how can I resolve this...
Thanks in Advance.
Posted
Comments
[no name] 20-Jul-12 7:09am    
Your collection class is probably marked private.
Rajesh Buddaraju 20-Jul-12 7:42am    
This class is System.Windows.Controls.SelectedItemCollection and it is protected only.

In your viewmodel you could add a public property that returns the count of the selectedItemCollection and use that property instead.
 
Share this answer
 
Comments
Rajesh Buddaraju 20-Jul-12 8:57am    
While debugging I can get the count value like this,
((SelectedItemCollection)obj).Count

but If I declared the same thing and It won't be able to build and I am getting the error as,
Error 9 'System.Windows.Controls.SelectedItemCollection' is inaccessible due to its protection level D:\Projects\GMS\GMSDesktop\Src\Modules\xxx.Security\ViewModels\xxx.cs 254 52
V. 20-Jul-12 10:16am    
yes, the ´watch´ during debugging allows access to protected and private members. Those are not accessible during normal runtime.
Rajesh Buddaraju 20-Jul-12 14:17pm    
V.Is there any alternative to handle this kind of list(selectedItemCollection) properties.
V. 21-Jul-12 15:59pm    
I never worked with the viewmodel before, so I have no idea how your code is implemented. The ´easy´ way would be to make everything public, which is a very bad idea. It might be a better option to put the neccesarty selecteditems properties into an object and pass the object down to where you want to go. Note that the viewmodel is not really part of the user interface and as such should not know anything directly about any controls.
To do binding you need to bind to a property, and it must be public. When you do not specify for a class, you get internal. Try to make the ViewModel class public and see if that works if still have a problem. I do know that internal classes work for ViewModels.
 
Share this answer
 
Comments
Rajesh Buddaraju 20-Jul-12 15:33pm    
Thanks for your reply! My Question is,
If I declared like below, I am getting an error.The error is,

Error: System.Windows.Controls.SelectedItemCollection' is inaccessible due to its protection level D:\Projects\GMS\GMSDesktop\Src\Modules\xxx.Security\ViewModels\xxx.cs 254 52
Code:
//for (int index = 1; index <= ((SelectedItemCollection)obj).Count; index++)
//{
// roleuser = obj(0);
// Available.Remove(roleuser);
// Selected.Add(roleuser);
//}
If you need any more information let me know! Thanks in advance for your support.
Assuming that the element of the collection parameter are of type MyObjectClass:

C#
List<MyObjectClass> SelectedItemsList{get;set;}
private void DataGridSelectionChanged(object selectedBackPlaneData)
{
   SelectedItemsList= (selectedBackPlaneData as IList).Cast<MyObjectClass>().ToList();
}

will do the trick.


You could also use a generic:
C#
private IList<t> ConvertListOfSelectedItem<t>(object param)
{
   return (param as IList).Cast<t>().ToList();
}


In which case the code would be

C#
IList<MyObjectClass> SelectedItemsList{get;set;}

private void DataGridSelectionChanged(object stuff)
{
   SelectedItemsList= ConvertListOfSelectedItem<MyObjectClass>(stuff);
}
 
Share this answer
 
v2
Comments
CHill60 3-Nov-14 15:17pm    
You're more than 2 years late with this

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