简体   繁体   English

如何从CollectionViewSource获取项目数?

[英]How to get Items count from CollectionViewSource?

I am using CollectionViewSource to filter the records displayed in a ListBox. 我使用CollectionViewSource来过滤ListBox中显示的记录。 The xaml follows. xaml如下。

   <Window x:Class="WPFStarter.ListBoxItemsFilter.ListBoxFilterUsingCollectionViewSource"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="userControl"
        Title="ListBoxFilterUsingCollectionViewSource" Height="300" Width="300">
        <Window.Resources>
        <CollectionViewSource Source="{Binding ElementName=userControl, Path=DataContext.Items}"
                              x:Key="cvs" Filter="CollectionViewSource_Filter"/>
        </Window.Resources>
    <StackPanel Orientation="Vertical">
        <TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged"/>
        <TextBlock x:Name="txtSummary" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Bottom"  FontSize="8"></TextBlock>
        <ListBox ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="First"/>
    </StackPanel>

</Window>

And here is my code-behing ( please don;t mind this code-behind, in the real application i am using the best of MVVM for this scenario). 这是我的代码behing(请不要介意这个代码隐藏,在真实的应用程序中我使用最好的MVVM来实现这个场景)。

 public partial class ListBoxFilterUsingCollectionViewSource : Window
    {
        private string _text="";
        private readonly CollectionViewSource _viewSource;

        public ListBoxFilterUsingCollectionViewSource()
        {
            InitializeComponent();
            _viewSource = this.FindResource("cvs") as CollectionViewSource;
        }

        private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
        {
            var character = e.Item as Character;
            e.Accepted = character != null && character.First.ToLower().Contains(_text.ToLower());
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            _text = txtSearch.Text;
            _viewSource.View.Refresh();

            SetSummary();
        }

        private void SetSummary()
        {                
            var initialCount = 10; //HELP????
            var filteredCount = 10; //HELP????
            txtSummary.Text = String.Format("{0} of {1}", filteredCount, initialCount);
        }
    }

QUESTION: I Need help in writing the "SetSummary" method, wherein i can get the "initialCount" and the "filteredCount" from CollectionViewSource object. 问题:我需要帮助编写“SetSummary”方法,其中我可以从CollectionViewSource对象获取“initialCount”和“filteredCount”。

Thanks for your interest. 谢谢你的关注。

你也可以做_viewSource.View.Cast<object>().Count()用于过滤列表和_viewSource.View.SourceCollection.Cast<object>().Count()的原件。

I think the better solution is, as usual, Linq! 我认为更好的解决方案就像往常一样,Linq!

_viewSource.View.Cast<[your_type]>().Count();

...or... ...要么...

_viewSource.View.Cast<object>().Count();

...if you don't know the items' type at runtime! ...如果您在运行时不知道项目的类型!

The source collection and collectionview both implements IEnumerable so you can always iterate over them and count how many are in them. 源集合和集合视图都实现了IEnumerable,因此您可以始终迭代它们并计算其中的数量。 But I would only recommend doing this if you have no access to the actual collection you used as source. 但是,如果您无法访问用作源的实际集合,我建议您执行此操作。

private void SetSummary() 
{
    int initialCount = 0;
    foreach(var item in _viewSource.View.SourceCollection)
    {
        initialCount++;
    }

    int filteredCount = 0;
    foreach (var item in _viewSource.View)
    {
        filteredCount++;
    }
}

If you're doing MVVM, you could have your VM create a collection view rather than one being created on your behalf by the CollectionViewSource . 如果您正在使用MVVM,则可以让您的VM创建集合视图,而不是CollectionViewSource代表您创建集合视图。 Then, you have control over what type of CVS is created, so you can create a ListCollectionViewSource , which has a Count property. 然后,您可以控制创建的CVS类型,因此您可以创建具有Count属性的ListCollectionViewSource It really depends on the properties of the data you're filtering. 这实际上取决于您要过滤的数据的属性。

var count = DataGrid.ItemsSource.OfType<object>().Count();
public static int Count(this ICollectionView view)
    {
        var index = 0;
        foreach (var unused in view)
        {
            index++;
        }
        return index;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM