• 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>
    

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

  • 相关阅读:
    Asp.Net 构架(Http Handler 介绍) Part.2<转>
    大数据插入<转>
    网站开发技巧参考大全<转>
    qq校友好东西
    SQL2005存储过程解密 <转>
    用Lucene.net对数据库建立索引及搜索<转>
    用SharpDevelop3调试ASP.NET的方法
    【转】卢彦的利用xml实现通用web报表打印
    张仰彪第二排序法 <转>
    依赖注入那些事儿 <转>
  • 原文地址:https://www.cnblogs.com/gongchuangsu/p/4872743.html
Copyright © 2020-2023  润新知