假设数据库有如下表,
首先我们创建一个WPF工程,界面如下
1 <Window x:Class="WpfApplication2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApplication2" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 9 <Grid> 10 <ListView x:Name="listview"> 11 <ListView.View> 12 <GridView x:Name="gridview"></GridView> 13 </ListView.View> 14 </ListView> 15 </Grid> 16 </Window>
然后创建配置文件。这里我用的是XML文件,
这里我们假设只显示Name,Height,Age三个字段 ,所以配置文件中只增加三项。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <List> <Item Header="Name" Width="100"></Item> <Item Header="Height" Width="80"></Item> <Item Header="Age" Width="80"></Item> </List>
下面是实现代码
1 using System; 2 using System.Collections.Generic; 3 using System.Windows; 4 using System.Windows.Controls; 5 using System.Windows.Data; 6 using System.Xml.Linq; 7 using System.Xml.XPath; 8 using System.Data; 9 using System.Data.SqlClient; 10 using System.Configuration; 11 using System.Globalization; 12 13 namespace WpfApplication2 14 { 15 /// <summary> 16 /// MainWindow.xaml 的交互逻辑 17 /// </summary> 18 public partial class MainWindow : Window 19 { 20 public MainWindow() 21 { 22 InitializeComponent(); 23 } 24 25 private void Window_Loaded(object sender, RoutedEventArgs e) 26 { 27 //动态添加项 28 DynamicAddItem(); 29 //显示数据 30 DataTable dt = GetData(); 31 this.listview.ItemsSource = dt.DefaultView; 32 33 } 34 35 /// <summary> 36 /// 动态添加列表项 37 /// </summary> 38 private void DynamicAddItem() 39 { 40 List<TestItem> list = GetAllItems(); 41 foreach (var item in list) 42 { 43 GridViewColumn column = new GridViewColumn(); 44 column.Header = item.Header; 45 column.Width = item.Width; 46 if (item.Header == "Age") 47 { 48 //有时候我们可能会用到数据转换 49 //这里以年龄后面加个岁为例 50 AgeConverter converter = new AgeConverter(); 51 column.DisplayMemberBinding = new Binding(item.Header) { Converter = converter}; 52 } 53 else 54 { 55 column.DisplayMemberBinding = new Binding(item.Header); 56 } 57 this.gridview.Columns.Add(column); 58 } 59 } 60 61 /// <summary> 62 /// 从配置文件读取要显示的项 63 /// </summary> 64 /// <returns></returns> 65 private List<TestItem> GetAllItems() 66 { 67 List<TestItem> list = new List<TestItem>(); 68 XDocument doc = XDocument.Load("list.xml"); 69 var result = doc.XPathSelectElements("List/Item"); 70 if(result != null) 71 { 72 foreach (var item in result) 73 { 74 TestItem ti = new TestItem(); 75 ti.Header = item.Attribute("Header").Value; 76 ti.Width = Convert.ToInt32(item.Attribute("Width").Value); 77 list.Add(ti); 78 } 79 } 80 return list; 81 } 82 83 /// <summary> 84 /// 从数据库获取数据 85 /// </summary> 86 /// <returns></returns> 87 private DataTable GetData() 88 { 89 DataTable dt = new DataTable(); 90 string sql = ""; 91 string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; 92 try 93 { 94 SqlConnection con = new SqlConnection(conStr); 95 sql = "select * from Student"; 96 SqlDataAdapter sda = new SqlDataAdapter(sql,con); 97 sda.Fill(dt); 98 return dt; 99 } 100 catch(Exception ex) 101 { 102 MessageBox.Show(ex.Message); 103 return dt; 104 } 105 } 106 } 107 108 struct TestItem 109 { 110 public string Header { get; set; } 111 112 public int Width { get; set; } 113 } 114 115 public class AgeConverter : IValueConverter 116 { 117 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 118 { 119 return value + "岁"; 120 } 121 122 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 123 { 124 throw new NotImplementedException(); 125 } 126 } 127 128 129 130 }
运行效果