开发者

Collection to Count Converter

开发者 https://www.devze.com 2023-03-17 09:45 出处:网络
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,

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,

Dict开发者_运维知识库ionary, 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;
}


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


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();
    }
}


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;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消