WPF过滤CollectionViewSource

时间:2013-06-04 11:24:46

标签: wpf dataset wpfdatagrid

我有一个从数据集中提取数据的WPF项目。在一个特定的视图中,我有一个网格,我想显示数据的过滤版本

我读过我无法过滤BindingListView,但这是一个CollectionViewSource

我的代码是: -

Private Sub UserControl_Loaded_1(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded

 Dim _ds As CantileverDataSet
 Dim _carcasseTableAdapter As CantileverDataSetTableAdapters.MaterialsTableAdapter
 Dim _carcasseViewSource As CollectionViewSource

 _ds = CType(Me.FindResource("CantileverDataSet"), CantileverDataSet)

 _carcasseTableAdapter = New CantileverDataSetTableAdapters.MaterialsTableAdapter
 _carcasseTableAdapter.Fill(_ds.Materials)
 _carcasseViewSource = CType(Me.FindResource("MaterialsViewSource"), CollectionViewSource)

 AddHandler _carcasseViewSource.Filter, AddressOf CarcasseFilter
End Sub

Private Sub CarcasseFilter(ByVal sender As System.Object, ByVal e As FilterEventArgs)
    'Accept everything for now

    e.Accepted = True
End Sub

我得到的错误是

'System.Windows.Data.BindingListCollectionView'视图不支持过滤。

如果我做_carcasseViewSource.GetType我得到CollectionViewSource 如果我做_carcasseViewSource.View.GetType我得到BindingListCollectionView

就好像它将我的_carcasseViewSource视为BindingListCollectionView无论

以下是网格的XAML以防万一

    <Grid x:Name="CarcasseGrid" DataContext="{StaticResource MaterialsViewSource}" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="Material Code:" Grid.Column="0" Grid.Row="0"/>
        <TextBox x:Name="MaterialCodeTextBox" Grid.Column="1" Grid.Row="0" Text="{Binding MaterialCode, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
        <Label Content="Name:" Grid.Column="0" Grid.Row="1"/>
        <TextBox x:Name="NameTextBox" Grid.Column="1" Grid.Row="1" Text="{Binding Name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
    </Grid>

任何想法都将不胜感激

1 个答案:

答案 0 :(得分:0)

使用数据集/数据表时可以使用IBindingListView.Filter

查看this过滤表达式