c# - 如何从 CollectionViewSource 获取项目数?

标签 c# wpf collectionviewsource

我正在使用 CollectionViewSource 来过滤列表框中显示的记录。 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>

这是我的代码隐藏(请不要介意这个代码隐藏,在实际应用程序中,我正在为这种情况使用最好的 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);
        }
    }

问题: 我在编写“SetSummary”方法时需要帮助,其中我可以从 CollectionViewSource 对象获取“initialCount”和“filteredCount”。

感谢您的关注。

最佳答案

你也可以做 _viewSource.View.Cast<object>().Count()对于筛选列表和 _viewSource.View.SourceCollection.Cast<object>().Count()为原创。

关于c# - 如何从 CollectionViewSource 获取项目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3499903/

相关文章:

windows-phone-7 - SelectedItem 使用 CollectionViewSource 设置为第一个项目

c# - 使用 Redemption 以编程方式访问 "Offline Address book"

在带有日历类型应用程序的 xaml 中选择的 C# WPF MVVM 集

c# - 分组 ListView 中的项目不更改组

c# - CollectionViewSource.Source 的调度程序更新在同一线程上抛出错误的线程异常

c# - 如何成功实现WPF文本框验证?

c# - 如果我的配置文件缺少 log4net 配置部分,为什么我的应用程序会挂起?

c#类工厂新手

c# - 从 C# 中的原始像素数据创建的位图不会在 IE 中显示

wpf - ReactiveUI:无法让代码在后台线程上运行