没有datagrid的默认视图

时间:2013-05-15 12:53:31

标签: c# wpf xaml

我想过滤数据网格中加载的数据。此数据网格绑定到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;
    }

1 个答案:

答案 0 :(得分: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;
        }