将Collectionviewsource与WPF Datagrid和Entity Framework绑定

时间:2012-06-19 16:56:08

标签: entity-framework wpfdatagrid collectionviewsource

我在WPF和C#方面比较新。在这个example之后,我创建了一个带有数据网格和两个组合框(FileName和PartID)的表单,我想用它来过滤数据网格(surveyresponsesDataGrid)。

这是我的WPF表单的XAML:

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
    Title="Review Data" Height="446" Width="987" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:SWA_IFR" Loaded="Window_Loaded">
<Window.Resources>
    <CollectionViewSource 
        Source="{Binding ElementName=surveyresponses, Path=FileName.SelectedItem}" 
        x:Key="cvs" Filter="fn_Filter" 
        x:Name="srView"
        CollectionViewType="{x:Type dat:ListCollectionView}" />
</Window.Resources>


<Grid>
    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="247" HorizontalAlignment="Left" 
              ItemsSource="{Binding Source={StaticResource cvs}}" Margin="12,93,0,0" Name="surveyresponsesDataGrid" 
              RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="941">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="fileUploadNameColumn" Binding="{Binding Path=FileUploadName}" Header="File Upload Name" Width="*" />
            <DataGridTextColumn x:Name="participantIDColumn" Binding="{Binding Path=ParticipantID}" Header="Participant ID" Width="*" />
            <DataGridTextColumn x:Name="quesIDColumn" Binding="{Binding Path=QuesID}" Header="Ques ID" Width="*" />
            <DataGridTextColumn x:Name="quesTypeColumn" Binding="{Binding Path=QuesType}" Header="Ques Type" Width="*" />
            <DataGridTextColumn x:Name="scoreIntColumn" Binding="{Binding Path=ScoreInt}" Header="Score Int" Width="*" />
            <DataGridTextColumn x:Name="scoreTextColumn" Binding="{Binding Path=ScoreText}" Header="Score Text" Width="*" />
            <DataGridTextColumn x:Name="scoreDescrColumn" Binding="{Binding Path=ScoreDescr}" Header="Score Descr" Width="*" />
        </DataGrid.Columns>
    </DataGrid>
    <ComboBox Height="29" HorizontalAlignment="Left" Margin="184,17,0,0" Name="FileName" VerticalAlignment="Top" Width="211" SelectionChanged="FileName_SelectionChanged" />
    <Label Content="Filter by File Upload Name" Height="29" HorizontalAlignment="Left" Margin="14,17,0,0" Name="label2" VerticalAlignment="Top" Width="159" />
    <ComboBox Height="29" HorizontalAlignment="Left" Margin="184,52,0,0" Name="PartID" VerticalAlignment="Top" Width="211"  />
    <Label Content="Filter by Participant ID" Height="29" HorizontalAlignment="Left" Margin="14,52,0,0" Name="label1" VerticalAlignment="Top" Width="159" />
</Grid>

这是我的代码:

  

公共部分类ReviewData:Window

{
    SMEntities context = new SMEntities();

    public ReviewData()
    {
        InitializeComponent();
    }


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        var filename = context.surveyresponses.Select(f => f.FileUploadName).Distinct();
        this.FileName.ItemsSource = filename;

        var partid = context.surveyresponses.Select(p => p.ParticipantID).Distinct();
        this.PartID.ItemsSource = partid;

    }

    void fn_Filter(object sender, FilterEventArgs e)
    {
        if (e.Item is surveyresponse)
            e.Accepted = (e.Item as surveyresponse).FileUploadName.ToUpper().Contains(FileName.Text.ToUpper());
        else
            e.Accepted = true;
    }

    private void RefreshList()
    {
        if (surveyresponsesDataGrid.Items is CollectionView)
        {
            CollectionViewSource csv = (CollectionViewSource)FindResource("cvs");
            if (csv != null)
                csv.View.Refresh();
        }
    }

    private void srFilter_TextChanged(object sender, TextChangedEventArgs e)
    {
        RefreshList();
    }
} }

第一个问题是我认为在Window.Resource部分中没有正确设置CollectionViewSource,但我找不到关于什么是关闭的任何明确的指导。 EF类是调查响应,但不确定我是否正确设置了Path属性 - 当前它被设置为第一个组合框FileName的SelectedItem。我的目标是仅对与FileName组合框中的选择匹配的条目过滤数据网格。

第二个问题是我不确定如何设置辅助过滤器,这样一旦设置了第一个过滤器,选择PartId组合框将根据第二个字段(参与者ID)进一步过滤数据网格。

我遗漏了一些基本的东西而且看不出它是什么。

感谢您的指导。

0 个答案:

没有答案