如何从CollectionViewSource获取项目数?

Man*_*ani 14 c# wpf collectionviewsource

我使用CollectionViewSource来过滤ListBox中显示的记录.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>
Run Code Online (Sandbox Code Playgroud)

这是我的代码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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题: 我需要帮助编写"SetSummary"方法,其中我可以从CollectionViewSource对象获取"initialCount"和"filteredCount".

谢谢你的关注.

rhy*_*yek 38

您也可以_viewSource.View.Cast<object>().Count()为已过滤的列表和_viewSource.View.SourceCollection.Cast<object>().Count()原始列表执行此操作.


MAX*_*AXE 11

我认为更好的解决方案就像往常一样,Linq!

_viewSource.View.Cast<[your_type]>().Count();
Run Code Online (Sandbox Code Playgroud)

...要么...

_viewSource.View.Cast<object>().Count();
Run Code Online (Sandbox Code Playgroud)

...如果您在运行时不知道项目的类型!


Wal*_*mer 6

源集合和集合视图都实现了IEnumerable,因此您可以始终迭代它们并计算其中的数量.但是,如果您无法访问用作源的实际集合,我建议您执行此操作.

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

    int filteredCount = 0;
    foreach (var item in _viewSource.View)
    {
        filteredCount++;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果可见项目不是已过滤掉的项目的计数,则filteredCount 是数字。 (2认同)