【问题标题】:How to get Items count from CollectionViewSource?如何从 CollectionViewSource 获取项目计数?
【发布时间】:2010-08-17 06:39:59
【问题描述】:

我正在使用 CollectionViewSource 过滤列表框中显示的记录。 xml 如下。

   <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”。

感谢您的关注。

【问题讨论】:

    标签: c# wpf collectionviewsource


    【解决方案1】:

    您也可以对过滤后的列表使用_viewSource.View.Cast&lt;object&gt;().Count(),对原始列表使用_viewSource.View.SourceCollection.Cast&lt;object&gt;().Count()

    【讨论】:

    • 感谢您阐明如何获取 CollectionViewSource 的过滤/排序内容。在您发表评论之前,我无法获得 CollectionViewSource 中的第一项:var firstItem = this.xViewInDescendingOrder.View.Cast().ElementAt(0);
    【解决方案2】:

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

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

    ...或...

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

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

    【讨论】:

      【解决方案3】:

      源集合和集合视图都实现了 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++;
          }
      }
      

      【讨论】:

      • 请注意,filteredCount 是可见项目的数量,而不是已过滤掉的项目的计数。
      【解决方案4】:

      如果您使用 MVVM,您可以让您的 VM 创建一个集合视图,而不是由 CollectionViewSource 代表您创建一个集合视图。然后,您可以控制创建的 CVS 类型,因此您可以创建一个具有 Count 属性的 ListCollectionViewSource。这实际上取决于您要过滤的数据的属性。

      【讨论】:

      • 对不起,我无法得到它。您能否通过在上下文中使用我的代码来解释一些代码。谢谢。
      【解决方案5】:
      var count = DataGrid.ItemsSource.OfType<object>().Count();
      

      【讨论】:

      • 通过这个你可以得到 itemsSource 的数量例如 2 , 3
      • 虽然这段代码可以回答问题,但最好解释一下如何解决问题,并提供代码作为示例或参考。仅代码的答案可能会令人困惑且缺乏上下文。
      • 感谢 Robert Columbia 先生指导我以良好的方式提供帮助
      【解决方案6】:
      public static int Count(this ICollectionView view)
          {
              var index = 0;
              foreach (var unused in view)
              {
                  index++;
              }
              return index;
          }
      

      【讨论】:

      • 虽然这段代码可以回答问题,但最好解释一下如何解决问题,并提供代码作为示例或参考。仅代码的答案可能会令人困惑且缺乏上下文。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多