0

我想过滤加载到数据网格中的数据。此数据网格绑定到 EF 的实体。我在互联网上阅读了有关过滤的不同帖子,并且从所有这些帖子中我可以创建下面的代码。问题是我仍然无法得到我想要的。由于“MyData”为 NULL,因此引发异常。为什么 defaultview 不返回数据?

这是 XAML

<Grid Name="gG" DataContext="{StaticResource t_KlantenViewSource}">
    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Name="t_KlantenDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" CanUserReorderColumns="False" CanUserDeleteRows="False" CanUserAddRows="False" CanUserResizeRows="False" MouseDoubleClick="t_KlantenDataGrid_MouseDoubleClick" Margin="0,21,0,10" FontSize="13">

    </DataGrid>
    <TextBox Height="23" HorizontalAlignment="Right" Name="tbSearch" VerticalAlignment="Top" Width="120" Margin="0,-7,99.2,0" TextChanged="tbSearch_TextChanged" />
</Grid>

这是代码

CollectionViewSource t_KlantenViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("t_KlantenViewSource")));
        System.Data.Objects.ObjectQuery<AOV.t_Klanten> customersQuery = this.Getlt_KlantenQuery(aovent);
        t_KlantenViewSource.Source = customersQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);

private System.ComponentModel.ICollectionView MyData;
string SearchText = string.Empty;

    private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        MyData = CollectionViewSource.GetDefaultView(t_KlantenDataGrid.DataContext);
        TextBox t = sender as TextBox;
        SearchText = t.Text.ToString();
        MyData.Filter = FilterData;
    }
4

1 回答 1

0

您需要按照下面更新的代码中所述更改第一行。由于 GetDefaultView 接受绑定源。

 private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            MyData = CollectionViewSource.GetDefaultView(t_KlantenDataGrid.ItemsSource);
            TextBox t = sender as TextBox;
            SearchText = t.Text.ToString();
            MyData.Filter = FilterData;
        }
于 2015-10-29T11:40:22.930 回答