收集到计数转换器
如何将集合转换为计数?
当传递集合时,转换器应该能够
返回以下集合的计数: Dictionary
、ObservableCollection
或 List
立即 有以下但不起作用,
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((System.Collections.ICollection)value) != null ? ((System.Collections.ICollection)value).Count : 0;
}
How do i convert a collection to count?
Where when a collection is passed the converter should be able to return the count for say the following collections,
Dictionary
, ObservableCollection
or List
right now i have the following but doesn't work,
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((System.Collections.ICollection)value) != null ? ((System.Collections.ICollection)value).Count : 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关联的
ICollectionView
有一个Count
属性,您可以将其数据绑定到编辑:以下是访问相关 ICollectionView 的方法
WPF 系统为绑定到 ItemsSource 的集合创建 ICollectionView特性。您还可以使用 CollectionViewSource.GetDefaultView() 在代码隐藏中实例化 ICollectionViews。请参阅 msdn.microsoft.com/en-us/library/ms752284.aspx
另请注意,如果关联的源集合支持更改通知,则 Count 属性将发送更改通知
The associated
ICollectionView
has aCount
property that you can databind toEdit: Here's how you get access to the relevant ICollectionView
The WPF system creates an ICollectionView for collections that are bound to ItemsSource properties. You can also instantiate ICollectionViews in the code-behind by using CollectionViewSource.GetDefaultView(). See msdn.microsoft.com/en-us/library/ms752284.aspx
Also note that the Count property will send change notifications if the associated source collection supports change notifications
如果我们将“集合”定义为实现
ICollection 的东西
或ICollection
然后 Count 属性可以告诉我们集合中元素的数量。如果我们希望基于 ICollection.Count 计算某个值,但以某种方式进行修改,那么我们可以创建一个 通用方法为我们计算价值。例如,通用方法作为实际参数的函数来调用此类函数。
Convert
可以采用ICollection。 value
形式参数。可以使用任何实现 ICollection因为编译器可以推断泛型类型参数,所以我们在调用泛型方法时不需要显式指定类型参数(尽管如果我们愿意或需要的话可以这样做)。
例如...
If we define a "collection" as something that implements
ICollection
orICollection<T>
then the Count property is available to tell us the number of elements in the collection if that is what we need to know.If we wish to calculate some value based upon ICollection.Count but modified in some way then we can create a Generic Method to calculate the value for us. For example, a generic method
Convert<T>
could take anICollection<T> value
formal parameter. Such a function could be invoked using anything that implementsICollection<T>
as an actual parameter.Because the compiler can infer the generic type argument we don't need to explicitly specify the type argument when invoking the generic method (although we can do so if we want or need to).
For example...
这不是 ToList() 并获取计数的选项吗?
Is it not an option to ToList() and get the count?