如何从SelectedItemCollection获取计数 [英] How to get count from a SelectedItemCollection

查看:283
本文介绍了如何从SelectedItemCollection获取计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的viewmodel类在运行时中有一个对象。该obj包含列表框中的SelectedItemCollection。



问题是我可以在调试时获得运行时的计数。



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.

((SelectedItemCollection)obj).Count

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





但是当我在编译时宣布相同时我收到以下错误。

错误9System.Windows.Controls.SelectedItemCollection由于其保护级别而无法访问D:\Projects \ GMS \ GMSDesktop \Src \Modules \ xxx.Security \ViewModels \ xxxcs 254 52



伙计们,让我知道如何解决这个问题...

先谢谢你。



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.

推荐答案

在您的viewmodel中,您可以添加一个公共属性,该属性返回selectedItemCollection的计数并使用该属性。
In your viewmodel you could add a public property that returns the count of the selectedItemCollection and use that property instead.


要进行绑定,您需要绑定到财产, d必须公开。如果没有为类指定,则会获得内部。尝试将ViewModel类设置为public,并查看是否仍然有问题。我知道内部类适用于ViewModels。
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.


假设collection参数的元素是MyObjectClass类型:



Assuming that the element of the collection parameter are of type MyObjectClass:

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:

private IList<t> ConvertListOfSelectedItem<t>(object param)
{
   return (param as IList).Cast<t>().ToList();
}





在这种情况下,代码将是





In which case the code would be

IList<MyObjectClass> SelectedItemsList{get;set;}

private void DataGridSelectionChanged(object stuff)
{
   SelectedItemsList= ConvertListOfSelectedItem<MyObjectClass>(stuff);
}


这篇关于如何从SelectedItemCollection获取计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆