-->

CollectionViewSource,如何筛选数据?(CollectionViewSource,

2019-07-18 15:42发布

我绑定一个ComboBox实体,但我想要的数据过滤。

到现在为止我已经尝试了两种方法:

  • “简单的”一:直接应用过滤器的对象集throught LINQ到实体
  • 设置一个过滤事件处理程序上描述MSDN

我对第一种方法满足,首先是因为生成的数据库查询包含一个WHERE子句,所以不是所有的整个数据必须从远程数据库中检索....

然而,#2的方法是通过更为灵活,如果在运行时我想改变应用的过滤......我按照MSDN上的例子,但我得到一个例外,为什么?

所以,我的问题是:
1.哪种方法比较好
2.为什么我得到的异常?

这里是我的代码:

 private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        //Do not load your data at design time.
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            //Load your data here and assign the result to the CollectionViewSource.
            System.Windows.Data.CollectionViewSource myCollectionViewSource =
                (System.Windows.Data.CollectionViewSource)
                this.Resources["tSCHEDEViewSource"];

            // If I use this I get the data filtered on startup, but is it the right mode?
            //myCollectionViewSource.Source = _context.TSCHEDE.Where(s => s.KLINEA == kLinea && s.FCANC == "T").OrderBy(s => s.DSCHEDA).OrderByDescending(s => s.DSTORICO);

            // Instead If I apply my custom filtering logic
            myCollectionViewSource.Filter += new FilterEventHandler(filterSource);

            myCollectionViewSource.Source = _context.TSCHEDE; // ... Here i get an exception: 
            // 'System.Windows.Data.BindingListCollectionView' view does not support filtering. ???
        }
    }


    private void filterSource(object sender, FilterEventArgs e)
    {
        TSCHEDE scheda = e.Item as TSCHEDE;
        if (scheda != null)
        {
            if (scheda.KLINEA == 990)
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }
    }

编辑 :我曾尝试推行视图上的过滤器属性,而不是设置事件处理程序:

myCollectionView = (BindingListCollectionView)myCollectionViewSource.View;
myCollectionView.Filter = new Predicate<object>(Contains);

public bool Contains(object de)
    {
        TSCHEDE scheda = de as TSCHEDE;
        return (scheda.KLINEA == 990);
    }

而现在我得到的不是那么有用的例外:

不支持指定方法:System.NotSupportedException。 在System.Windows.Data.CollectionView.set_Filter(Predicate`1值)

编辑

XAML代码:

<UserControl.Resources>
    <CollectionViewSource x:Key="tSCHEDEViewSource" d:DesignSource="{d:DesignInstance my:TSCHEDE, CreateList=True}"  >
    </CollectionViewSource>
    <DataTemplate x:Key="SchedaTemplate">
        <StackPanel Orientation="Horizontal" >
            <TextBlock Text="{Binding Path=KSCHEDA}" Width="60"></TextBlock>
            <TextBlock Text="{Binding Path=DArticolo}" Width="200"></TextBlock>
            <TextBlock Text=" - " Width="40"></TextBlock>
            <TextBlock Text="{Binding Path=DSTORICO}" Width="150"></TextBlock>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
<Grid Background="PapayaWhip" DataContext="{StaticResource tSCHEDEViewSource}" DataContextChanged="StartHere" Name="rootGrid">
    <ComboBox ItemTemplate="{StaticResource SchedaTemplate}" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="23,129,0,0" Name="tSCHEDEComboBox1" SelectedValuePath="KSCHEDA" VerticalAlignment="Top" Width="393">
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>
    </ComboBox>
</Grid>

现在,我想这个问题是在XAML绑定,而不是在后面的代码...

Answer 1:

勾选此

1) 的CollectionView过滤

过滤需要基于委托(谓词),其上会发生过滤器。 谓词取入项目基于该值真或假返回,它选择或取消选择的元素。

this.Source.Filter = item => {
    ViewItem vitem = item as ViewItem;
    return vItem != null && vitem.Name.Contains("A");
};

2) 动态滤波数据



Answer 2:

铝最后我找到了一个解决方案,还张贴在这个问题上明确声明集合类型:

CollectionViewType = “的ListCollectionView”

所以在XAML加入收藏类型:

<CollectionViewSource x:Key="tSCHEDEViewSource" d:DesignSource="{d:DesignInstance my:TSCHEDE,  CreateList=True}" CollectionViewType="ListCollectionView">
    </CollectionViewSource>

而在现在的代码的事件处理程序的工作原理:

myCollectionViewSource.Filter += new FilterEventHandler(filterSource);

唯一遗憾的是,我没有在XAML明白为什么,什么东西显然是如此简单,我必须强迫它“手动” ??? 对我来说这似乎是一个黑客,也很容易出错...



文章来源: CollectionViewSource, how to filter data?