Binding to first N items in an ObservableCollection on a ListView

不羁岁月 提交于 2020-05-31 05:54:53

问题


I have an ObservableCollection<T> in my application that can hold numerous items. Specifically, this is summary information from a potentially extensive logger. What I'd like to do is bind strictly the top 3 items of this collection to WPF ListView.

Is there XAML syntax, or a simple enough way to create a secondary property in my VM, to always return the top 3 items in the collection as well as update any changes to the collection as a normal ObservableCollection<T> would if you were interacting with a complete list?


回答1:


You could roll-out your own ObservableCollection<T> with a property returning top items.

Here's an example,

Code:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow
    {
        public MyCollection Collection { get; }

        public MainWindow()
        {
            InitializeComponent();

            Collection = new MyCollection();

            DataContext = Collection;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Collection.Add(Collection.Count);
        }
    }

    public sealed class MyCollection : ObservableCollection<int>
    {
        public MyCollection()
        {
            CollectionChanged += MyCollection_CollectionChanged;
        }

        private void MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {   // to notify XAML-side binding
            OnPropertyChanged(new PropertyChangedEventArgs(nameof(TopItems)));
        }

        public IEnumerable<int> TopItems => this.Take(3);
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        d:DataContext="{d:DesignInstance local:MyCollection}"
        mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Click="button_Click" Content="Button" />
        <ListBox Grid.Row="1" ItemsSource="{Binding}" />
        <ListBox Grid.Row="2" ItemsSource="{Binding TopItems}" />
    </Grid>
</Window>



回答2:


You want to check this: INotifyPropertyChanged for Count property in WPF?

Simply listen on CollectionChanged of your ObservableCollection and there notify your viewmodel about changing your property holding only first 3 items (which can be implemented simply as

public MyTopItems
{
    get
    {
        return myCollection.Take(3);
    }
}



回答3:


you can also use a paged list

return myCollection.Skip(10).Take(3);


来源:https://stackoverflow.com/questions/43264213/binding-to-first-n-items-in-an-observablecollection-on-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!