c#-listview

listview通常和gridview结合起来使用,显示表格数据。

1.创建wpf应用程序,在mainwindow.xaml中使用。

<ListView Grid.Row="2" FontSize="20px" Name="ParientReport" Margin="10 0 10 10" BorderBrush="#FF717883" Foreground="Black" FontFamily="Microsoft YaHei">
            <ListView.Resources>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="CornerRadius" Value="20"/>
                    <Setter Property="BorderBrush" Value="#c1d0dc"/>
                </Style>
                <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
                <DataTemplate x:Key="myHeaderTemplate">
                    <DockPanel>
                        <TextBlock FontSize="20" Foreground="Black" FontWeight="Bold">
                            <TextBlock.Text>
                                <Binding/>
                            </TextBlock.Text>
                        </TextBlock>
                    </DockPanel>
                </DataTemplate>
                <DataTemplate x:Key="myCellTemplateMonth">
                    <DockPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                        <TextBlock Foreground="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <TextBlock.Text>
                                <Binding Path="ParientId"/>
                            </TextBlock.Text>
                        </TextBlock>
                    </DockPanel>
                </DataTemplate>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridViewColumn  Header="就诊号" Width="220" HeaderTemplate="{StaticResource myHeaderTemplate}" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center"  Text="{Binding Path=ParientId}" Width="220"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="姓名" Width="150" HeaderTemplate="{StaticResource myHeaderTemplate}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center"  Text="{Binding Path=Name}" Width="150"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="年龄" Width="150"  HeaderTemplate="{StaticResource myHeaderTemplate}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center"  Text="{Binding Path=Age}" Width="150"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="报告时间" Width="235" HeaderTemplate="{StaticResource myHeaderTemplate}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center"  Text="{Binding Path=ReportDate}" Width="235"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

2.在逻辑处理代码MainWindow.xaml.cs中进行数据绑定。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void PatientId_GotFocus(object sender, RoutedEventArgs e)
        {
            PatientId.Text = string.Empty;
            PatientId.Foreground = Brushes.Black;
        }

        private void PatientId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) 
            {
                List<ParientInfo> parientInfos = new List<ParientInfo>();
                ParientInfo parientInfo = new ParientInfo();
                parientInfo.ParientId = "20019871";
                parientInfo.Name = "测试用户";
                parientInfo.Age = "55";
                parientInfo.ReportDate = "2021-11-12 15:44";
                parientInfos.Add(parientInfo);
                ParientReport.ItemsSource = parientInfos;
            }
        }
    }
    //患者信息
    public class ParientInfo 
    {
        public string ParientId { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public string ReportDate { get; set; }
    }
}

3.效果预览

发表评论

您的电子邮箱地址不会被公开。

公众号

编程面试题