C# 绑定到集合的最后N个元素

C# 绑定到集合的最后N个元素,c#,wpf,data-binding,C#,Wpf,Data Binding,我使用wpf工具包图表来显示存储在可观察集合中的一些数据。当该集合中存储的项目超过N个时,只应显示最后N个项目(我无法删除任何项目) XAML <chartingToolkit:LineSeries DependentValueBinding="{Binding DoubleValue,Converter={StaticResource DoubleValueConverter}}" IndependentValueBinding="{Binding Count}" ItemsSource

我使用wpf工具包图表来显示存储在
可观察集合中的一些数据。当该集合中存储的项目超过N个时,只应显示最后N个项目(我无法删除任何项目)

XAML

<chartingToolkit:LineSeries DependentValueBinding="{Binding DoubleValue,Converter={StaticResource DoubleValueConverter}}" IndependentValueBinding="{Binding Count}" ItemsSource="{Binding Converter={StaticResource DataSourceConverter}}"/>

数据源转换程序

public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ICollection<object> items = value as ICollection<object>;

        int N = 300;

        if (items != null)
        {
            return items.Skip(Math.Max(0, items.Count - N)).Take(N);
        }
        else
        {
            return value;
        }  
    }
public object Convert(对象值,系统类型targetType,对象参数,系统全球化。文化信息文化)
{
ICollection items=作为ICollection的值;
int N=300;
如果(项!=null)
{
返回items.Skip(Math.Max(0,items.Count-N)).Take(N);
}
其他的
{
返回值;
}  
}

ItemSource绑定到同时包含“DoubleValue”和“Count”的
ObservableCollection
。似乎只有一次调用了
DataSourceConverter
,而在更新我的
ObservableCollection
时没有调用。

忘记转换器,在viewmodel类中创建一个新属性,它返回最后300项(就像您现在在转换器中声明的那样),并绑定到该项。

忘记转换器,在viewmodel类中创建一个新属性,该属性返回最后300项(就像您现在在转换器中声明的那样),并绑定到该属性。

您可以使用ICollectionView并在其上设置过滤器

从ObservableCollection创建一个新的CollectionView:

CollectionView topNItems = (CollectionView) CollectionViewSource.GetDefaultView(myObservableCollection);
topNItems.Filter += new FilterEventHandler(ShowOnlyTopNItems);
接下来,在CollectionView上创建过滤器:

CollectionView topNItems = (CollectionView) CollectionViewSource.GetDefaultView(myObservableCollection);
topNItems.Filter += new FilterEventHandler(ShowOnlyTopNItems);
最后是过滤器事件:

private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
    int n = 300;
    int listCount = myObservableCollection.Count;
    int indexOfItem = myObservableCollection.IndexOf(e.Item);
    e.Accepted = (listCount - indexOfItem) < n;
}
private void ShowOnlyBargainsFilter(对象发送方,FilterEventArgs e)
{
int n=300;
int listCount=myObservableCollection.Count;
int indexOfItem=myObservableCollection.IndexOf(e.Item);
e、 已接受=(listCount-indexOfItem)

现在,将图表绑定到新的topNItems,而不是ObservableCollection。

您可以使用ICollectionView并在其上设置过滤器

从ObservableCollection创建一个新的CollectionView:

CollectionView topNItems = (CollectionView) CollectionViewSource.GetDefaultView(myObservableCollection);
topNItems.Filter += new FilterEventHandler(ShowOnlyTopNItems);
接下来,在CollectionView上创建过滤器:

CollectionView topNItems = (CollectionView) CollectionViewSource.GetDefaultView(myObservableCollection);
topNItems.Filter += new FilterEventHandler(ShowOnlyTopNItems);
最后是过滤器事件:

private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
    int n = 300;
    int listCount = myObservableCollection.Count;
    int indexOfItem = myObservableCollection.IndexOf(e.Item);
    e.Accepted = (listCount - indexOfItem) < n;
}
private void ShowOnlyBargainsFilter(对象发送方,FilterEventArgs e)
{
int n=300;
int listCount=myObservableCollection.Count;
int indexOfItem=myObservableCollection.IndexOf(e.Item);
e、 已接受=(listCount-indexOfItem)

现在,将您的图表绑定到新的topNItems,而不是ObservableCollection。

只需进行另一次收集并将相关项目复制到其中。只需进行另一次收集并将相关项目复制到其中。我同意!我只想使用以下代码:
CollectionViewSource CollectionViewSource=newcollectionviewsource();collectionViewSource.Source=viewModel.YourObservableCollection;collectionViewSource.Filter+=/**/collectionViewSource.View
属性。我同意!我只想使用以下代码:
CollectionViewSource CollectionViewSource=newcollectionviewsource();collectionViewSource.Source=viewModel.YourObservableCollection;collectionViewSource.Filter+=/**/collectionViewSource.View
属性。