listview
通常和gridview
结合起来使用,显示表格数据。
1.创建wpf应用程序,在mainwindow.xaml中使用。
<ListViewGrid.Row="2"FontSize="20px"Name="ParientReport"Margin="10 0 10 10"BorderBrush="#FF717883"Foreground="Black"FontFamily="Microsoft YaHei"><ListView.Resources><StyleTargetType="{x:Type Border}"><SetterProperty="CornerRadius"Value="20"/><SetterProperty="BorderBrush"Value="#c1d0dc"/></Style><Stylex:Key="ListViewItemStyle"TargetType="{x:Type ListViewItem}"><SetterProperty="HorizontalContentAlignment"Value="Stretch"/></Style><DataTemplatex:Key="myHeaderTemplate"><DockPanel><TextBlockFontSize="20"Foreground="Black"FontWeight="Bold"><TextBlock.Text><Binding/></TextBlock.Text></TextBlock></DockPanel></DataTemplate><DataTemplatex:Key="myCellTemplateMonth"><DockPanelHorizontalAlignment="Center"VerticalAlignment="Center"><TextBlockForeground="Black"HorizontalAlignment="Stretch"VerticalAlignment="Stretch"><TextBlock.Text><BindingPath="ParientId"/></TextBlock.Text></TextBlock></DockPanel></DataTemplate></ListView.Resources><ListView.View><GridView><GridViewColumnHeader="就诊号"Width="220"HeaderTemplate="{StaticResource myHeaderTemplate}"><GridViewColumn.CellTemplate><DataTemplate><TextBlockTextAlignment="Center"Text="{Binding Path=ParientId}"Width="220"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn><GridViewColumnHeader="姓名"Width="150"HeaderTemplate="{StaticResource myHeaderTemplate}"><GridViewColumn.CellTemplate><DataTemplate><TextBlockTextAlignment="Center"Text="{Binding Path=Name}"Width="150"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn><GridViewColumnHeader="年龄"Width="150"HeaderTemplate="{StaticResource myHeaderTemplate}"><GridViewColumn.CellTemplate><DataTemplate><TextBlockTextAlignment="Center"Text="{Binding Path=Age}"Width="150"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn><GridViewColumnHeader="报告时间"Width="235"HeaderTemplate="{StaticResource myHeaderTemplate}"><GridViewColumn.CellTemplate><DataTemplate><TextBlockTextAlignment="Center"Text="{Binding Path=ReportDate}"Width="235"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn></GridView></ListView.View></ListView>
2.在逻辑处理代码MainWindow.xaml.cs中进行数据绑定。
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Data; usingSystem.Windows.Documents; usingSystem.Windows.Input; usingSystem.Windows.Media; usingSystem.Windows.Media.Imaging; usingSystem.Windows.Navigation; usingSystem.Windows.Shapes; namespaceReportManager{ /// <summary>/// Interaction logic for MainWindow.xaml/// </summary>publicpartialclassMainWindow : Window { publicMainWindow() { InitializeComponent(); } privatevoidPatientId_GotFocus(objectsender, RoutedEventArgse) { PatientId.Text=string.Empty; PatientId.Foreground=Brushes.Black; } privatevoidPatientId_KeyDown(objectsender, KeyEventArgse) { if (e.Key==Key.Enter) { List<ParientInfo>parientInfos=newList<ParientInfo>(); ParientInfoparientInfo=newParientInfo(); parientInfo.ParientId="20019871"; parientInfo.Name="测试用户"; parientInfo.Age="55"; parientInfo.ReportDate="2021-11-12 15:44"; parientInfos.Add(parientInfo); ParientReport.ItemsSource=parientInfos; } } } //患者信息publicclassParientInfo { publicstringParientId { get; set; } publicstringName { get; set; } publicstringAge { get; set; } publicstringReportDate { get; set; } } }
3.效果预览