收集到计数转换器

发布于 2024-11-18 08:56:59 字数 393 浏览 5 评论 0原文

如何将集合转换为计数?

当传递集合时,转换器应该能够

返回以下集合的计数: DictionaryObservableCollectionList

立即 有以下但不起作用,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杯别 2024-11-25 08:56:59

关联的 ICollectionView 有一个 Count 属性,您可以将其数据绑定到

编辑:以下是访问相关 ICollectionView 的方法

WPF 系统为绑定到 ItemsSource 的集合创建 ICollectionView特性。您还可以使用 CollectionViewSource.GetDefaultView() 在代码隐藏中实例化 ICollectionViews。请参阅 msdn.microsoft.com/en-us/library/ms752284.aspx

另请注意,如果关联的源集合支持更改通知,则 Count 属性将发送更改通知

The associated ICollectionView has a Count property that you can databind to

Edit: 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

橙幽之幻 2024-11-25 08:56:59

如果我们将“集合”定义为实现 ICollection 的东西ICollection然后 Count 属性可以告诉我们集合中元素的数量。

如果我们希望基于 ICollection.Count 计算某个值,但以某种方式进行修改,那么我们可以创建一个 通用方法为我们计算价值。例如,通用方法Convert可以采用ICollection。 value 形式参数。可以使用任何实现 ICollection作为实际参数的函数来调用此类函数。

因为编译器可以推断泛型类型参数,所以我们在调用泛型方法时不需要显式指定类型参数(尽管如果我们愿意或需要的话可以这样做)。

例如...

class Program
{
    static public int Convert<T>(ICollection<T> value, Type targetType, object parameter, CultureInfo culture)        {
        return value.Count;
    }

    static void Main(string[] args)
    {
        Dictionary<int, string> di = new Dictionary<int,string>();
        di.Add(1, "One");
        di.Add(2, "Two");
        di.Add(3, "Three");
        Console.WriteLine("Dictionary count: {0}", di.Count);
        Console.WriteLine("Dictionary Convert: {0}", Convert(di, null, null, null));

        ObservableCollection<double> oc = new ObservableCollection<double>();
        oc.Add(1.0);
        oc.Add(2.0);
        oc.Add(3.0);
        oc.Add(4.0);
        Console.WriteLine("ObservableCollection Count: {0}", oc.Count);
        Console.WriteLine("ObservableCollection Convert: {0}", Convert(oc, null, null, null));

        List<string> li = new List<string>();
        li.Add("One");
        li.Add("Two");
        li.Add("Three");
        li.Add("Four");
        li.Add("Five");
        Console.WriteLine("List Count: {0}", li.Count);
        Console.WriteLine("List Convert: {0}", Convert(li, null, null, null));

        Console.ReadLine();
    }
}

If we define a "collection" as something that implements ICollection or ICollection<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 an ICollection<T> value formal parameter. Such a function could be invoked using anything that implements ICollection<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...

class Program
{
    static public int Convert<T>(ICollection<T> value, Type targetType, object parameter, CultureInfo culture)        {
        return value.Count;
    }

    static void Main(string[] args)
    {
        Dictionary<int, string> di = new Dictionary<int,string>();
        di.Add(1, "One");
        di.Add(2, "Two");
        di.Add(3, "Three");
        Console.WriteLine("Dictionary count: {0}", di.Count);
        Console.WriteLine("Dictionary Convert: {0}", Convert(di, null, null, null));

        ObservableCollection<double> oc = new ObservableCollection<double>();
        oc.Add(1.0);
        oc.Add(2.0);
        oc.Add(3.0);
        oc.Add(4.0);
        Console.WriteLine("ObservableCollection Count: {0}", oc.Count);
        Console.WriteLine("ObservableCollection Convert: {0}", Convert(oc, null, null, null));

        List<string> li = new List<string>();
        li.Add("One");
        li.Add("Two");
        li.Add("Three");
        li.Add("Four");
        li.Add("Five");
        Console.WriteLine("List Count: {0}", li.Count);
        Console.WriteLine("List Convert: {0}", Convert(li, null, null, null));

        Console.ReadLine();
    }
}
辞取 2024-11-25 08:56:59

这不是 ToList() 并获取计数的选项吗?

static void Main(string[] args)
        {
            IDictionary<int,int> s = new Dictionary<int, int>();
            ObservableCollection<int> s1 = new ObservableCollection<int>();
            IList<int> s2 = new List<int>();

            int count = GetCount(s.ToList());
            int count1 = GetCount(s1.ToList());
            int count2 = GetCount(s2.ToList());

        }


 private static int GetCount(ICollection s)
        {
            return s.Count;
        }

Is it not an option to ToList() and get the count?

static void Main(string[] args)
        {
            IDictionary<int,int> s = new Dictionary<int, int>();
            ObservableCollection<int> s1 = new ObservableCollection<int>();
            IList<int> s2 = new List<int>();

            int count = GetCount(s.ToList());
            int count1 = GetCount(s1.ToList());
            int count2 = GetCount(s2.ToList());

        }


 private static int GetCount(ICollection s)
        {
            return s.Count;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文