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>

你可能感兴趣的:(数据库,ListView,listbox)