我在 WPF 和 C# 方面相对较新。按照这个示例,我创建了一个带有数据网格和两个组合框(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 :窗口
{ 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 类是surveyresponses,但不确定我是否正确设置了Path 属性——目前它设置为第一个组合框FileName 的SelectedItem。我的目标是仅过滤与 FileName 组合框中的选择匹配的条目的数据网格。
第二个问题是我不确定如何使用辅助过滤器,这样一旦设置了第一个过滤器,选择 PartId 组合框将根据第二个字段(参与者 ID)进一步过滤数据网格。
我错过了一些基本的东西,看不出它是什么。
感谢您的指导。