• ItemsControl 类绑定数据库


    // .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;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    
    namespace ListView
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            // 定义连接字符串
            static public string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            // 创建 Connection 对象
            static public SqlConnection conn = new SqlConnection(connString);
    
            public MainWindow()
            {
                InitializeComponent();
    
                string sql = String.Format("select * from t_xtgl_czry");
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    listCustomer.Add(new UserInfo(dr["rybm"].ToString(), dr["name"].ToString(), dr["qx"].ToString()));
                }
                lv.ItemsSource = listCustomer;
                dr.Close();
                conn.Close();
                // 设置默认选中第一项
                lv.SelectedIndex = 0;
            }
    
            public class UserInfo : INotifyPropertyChanged
            {
                #region INotifyPropertyChanged 成员
    
                public event PropertyChangedEventHandler PropertyChanged;
                public void OnPropertyChanged(PropertyChangedEventArgs e)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, e);
                    }
                }
                #endregion
                private string _employeeNumber;
                private string _Name;
                private string _Competence;
    
                public string EmployeeNumber
                {
                    get { return _employeeNumber; }
                    set { _employeeNumber = value; OnPropertyChanged(new PropertyChangedEventArgs("EmployeeNumber")); }
                }
                public string Name
                {
                    get { return _Name; }
                    set { _Name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); }
                }
    
                public string Competence
                {
                    get { return _Competence; }
                    set { _Competence = value; OnPropertyChanged(new PropertyChangedEventArgs("Competence")); }
                }
    
                public UserInfo(string empnumber, string name, string Competence)
                {
                    _employeeNumber = empnumber;
                    _Name = name;
                    _Competence = Competence;
                }
            }
            ObservableCollection<UserInfo> listCustomer = new ObservableCollection<UserInfo>();        
        }
    }
    
    // .xaml
    <Window x:Class="ListView.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:c="clr-namespace:ListView"        
            Title="ListViewSample" Height="305" Width="372">
    
        <Grid>
            <ListView Name="lv" HorizontalAlignment="Left" Height="155" Margin="34,88,0,0" VerticalAlignment="Top" Width="300">
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=EmployeeNumber}" Header="人员编码" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="姓名" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Competence}" Header="权限" Width="100"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Label Content="FullName:" HorizontalAlignment="Left" Margin="34,49,0,0" VerticalAlignment="Top" Width="71" Height="25"/>
            <TextBox HorizontalAlignment="Left" Height="25" Margin="119,49,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="211" VerticalContentAlignment="Center"/>
        </Grid>
    </Window>
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    一些关于视频相关基础概念
    熟悉某项目代码---零碎小知识总结
    C#中如何判断一个字符是汉字
    面试碰到一个这样的题------ 输入为一个字符串和字节数,输出为按字节截取的字符串
    C# 拼Json格式字符串 返回前段js 出错解决
    学习maple
    格林公式
    麦克斯韦方程组 (Maxwell's equation)的简单解释
    关于Ciarlet的泛函的一道homework的一个想法
    关于分开编写多个LaTeX文件的一点微小的总结
  • 原文地址:https://www.cnblogs.com/gongchuangsu/p/4872743.html
Copyright © 2020-2023  润新知