C#-ListView

简介: C# ListView

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.效果预览

目录
相关文章
|
7月前
|
C#
45.c#:listview控件
45.c#:listview控件
61 1
C#-ListView的使用
ListView顾名思义用来做列表数据展示,也是我们在开发中经常使用的控件之一,接下来将展示下它的一些使用场景,以满足不同的需求。
160 0
|
前端开发 C#
【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header
原文:【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header 一个ListView的MVVM简单例子: ...
1613 0
如何在 C#中的listView 控件中显示图片?
如何在 C#中的listView 控件中显示图片?
1256 0
如何在 C#中的listView 控件中显示图片?
|
存储 C#
C#(三十)之C#comboBox ListView treeView
本篇内容记录了comboBox下拉列表框属性、ListView 列表视图属性、ListView 列表视图事件、treeView树状视图属性。
274 0
C#(三十)之C#comboBox ListView treeView
|
C# 索引 Windows
C#编程-54:ListView控件复习笔记
C#编程-54:ListView控件复习笔记
132 0
|
C# 索引
C# 获取listview中选中一行的值
C# 获取listview中选中一行的值
865 0