我想将患者数据保存在xml文件中,如上图所示
<Patients>
<Patient name="Joao" bi="12345678" age="21" allergies="Pollen" address="" cellphone="961556408" medication="" sex="Male" />
</Patients>
我想在ListView上显示患者,但我想制作一个功能,用户可以通过姓名,出生日期,手机或商务智能过滤患者,但我不知道如何做到这一点,因为数据在XML文件上。
到目前为止我所拥有的: XAML:
<DockPanel Margin="2.4,7.2,8,1.4" Grid.Column="2" Grid.Row="2">
<TextBox DockPanel.Dock="Top" Margin="0,0,541.2,10" Name="txtFilter" TextChanged="txtFilter_TextChanged" />
<DatePicker x:Name="datePicker" DockPanel.Dock="Top" Margin="50,-27,541.2,10" Height="26" VerticalAlignment="Top"/>
<ComboBox DockPanel.Dock="Top" Name="SearchCombo" SelectedValuePath="Content" Margin="380,-45,279.2,10" Width="120" SelectionChanged="ComboBox_SelectionChanged" Height="21">
<ComboBoxItem Content="Name"></ComboBoxItem>
<ComboBoxItem Content="date of birth"></ComboBoxItem>
<ComboBoxItem Content="Cellphone"></ComboBoxItem>
<ComboBoxItem Content="BI"></ComboBoxItem>
</ComboBox>
<ListView Name="lvPatients" ItemsSource="{Binding Source={StaticResource PatientsData}, XPath=Patient}" SelectionChanged="lvPatients_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding XPath =@name}" />
<GridViewColumn Header="BI" Width="100" DisplayMemberBinding="{Binding XPath = @bi}" />
<GridViewColumn Header="Cellphone" Width="100" DisplayMemberBinding="{Binding XPath = @cellphone}"/>
<GridViewColumn Header="Sex type" Width="100" DisplayMemberBinding="{Binding XPath = @sex}"/>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
C#代码: 组合代码:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvPatients.ItemsSource);
switch (SearchCombo.SelectedValue.ToString())
{
case "Name":
datePicker.Visibility = Visibility.Hidden; //Shows the date picker
txtFilter.Visibility = Visibility.Visible; // Hides the text box
view.Filter = PatientNameFilter;
break;
case "Date of birth":
datePicker.Visibility = Visibility.Visible; //Shows the date picker
txtFilter.Visibility = Visibility.Hidden; // Hides the text box
view.Filter = PatientDateFilter;
break;
case "BI":
datePicker.Visibility = Visibility.Hidden; //Shows the date picker
txtFilter.Visibility = Visibility.Visible; // Hides the text box
view.Filter = PatientBIFilter;
break;
case "Cellphone":
datePicker.Visibility = Visibility.Hidden; //Shows the date picker
txtFilter.Visibility = Visibility.Visible; // Hides the text box
view.Filter = PatientPhoneFilter; // Cellphone
break;
}
}
所以问题是我不知道如何过滤listView上的数据。例如,如果我写&#34; Joao&#34;在texbox上,listView应该只显示患者&#34; Joao&#34;以及与他/她有关的一切(姓名,出生日期,性别等)。
提前致谢!
答案 0 :(得分:2)
您想要使用绑定到CollectionViewSource
的{{1}}。这将允许动态过滤。
这是一篇博客文章,解释了如何执行此操作:
https://grantwinney.com/using-a-textbox-and-collectionviewsource-to-filter-a-listview-in-wpf/
文件:
https://msdn.microsoft.com/en-us/library/system.windows.data.collectionview(v=vs.110).aspx
此外,使用直接XML可能会在将属性值转换为其本机类型时遇到一些问题。参见:
How do I use CollectionViewSource to sort xml records by date?
此外,