如何从 CollectionViewSource 获取项目数?

发布于 2024-09-14 15:15:33 字数 2392 浏览 3 评论 0原文

我正在使用 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”。

感谢您的关注。

I am using CollectionViewSource to filter the records displayed in a ListBox. The xaml follows.

   <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).

 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.

Thanks for your interest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

成熟稳重的好男人 2024-09-21 15:15:33

您还可以对过滤列表执行 _viewSource.View.Cast().Count() 和 _viewSource.View.SourceCollection.Cast().Count()< /code> 为原始。

You could also do _viewSource.View.Cast<object>().Count() for the filtered list and _viewSource.View.SourceCollection.Cast<object>().Count() for the original.

2024-09-21 15:15:33

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

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

...或者...

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

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

I think the better solution is, as usual, Linq!

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

...or...

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

...if you don't know the items' type at runtime!

随梦而飞# 2024-09-21 15:15:33

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

The source collection and collectionview both implements IEnumerable so you can always iterate over them and count how many are in them. 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++;
    }
}
叹倦 2024-09-21 15:15:33

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

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. Then, you have control over what type of CVS is created, so you can create a ListCollectionViewSource, which has a Count property. It really depends on the properties of the data you're filtering.

春庭雪 2024-09-21 15:15:33
var count = DataGrid.ItemsSource.OfType<object>().Count();
var count = DataGrid.ItemsSource.OfType<object>().Count();
是伱的 2024-09-21 15:15:33
public static int Count(this ICollectionView view)
    {
        var index = 0;
        foreach (var unused in view)
        {
            index++;
        }
        return index;
    }
public static int Count(this ICollectionView view)
    {
        var index = 0;
        foreach (var unused in view)
        {
            index++;
        }
        return index;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文