假设数据库有如下表,
首先我们创建一个WPF工程,界面如下
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<ListView x:Name="listview">
<ListView.View>
<GridView x:Name="gridview"></GridView>
</ListView.View>
</ListView>
</Grid>
</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>
下面是实现代码
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization; namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
//动态添加项
DynamicAddItem();
//显示数据
DataTable dt = GetData();
this.listview.ItemsSource = dt.DefaultView; } /// <summary>
/// 动态添加列表项
/// </summary>
private void DynamicAddItem()
{
List<TestItem> list = GetAllItems();
foreach (var item in list)
{
GridViewColumn column = new GridViewColumn();
column.Header = item.Header;
column.Width = item.Width;
if (item.Header == "Age")
{
//有时候我们可能会用到数据转换
//这里以年龄后面加个岁为例
AgeConverter converter = new AgeConverter();
column.DisplayMemberBinding = new Binding(item.Header) { Converter = converter};
}
else
{
column.DisplayMemberBinding = new Binding(item.Header);
}
this.gridview.Columns.Add(column);
}
} /// <summary>
/// 从配置文件读取要显示的项
/// </summary>
/// <returns></returns>
private List<TestItem> GetAllItems()
{
List<TestItem> list = new List<TestItem>();
XDocument doc = XDocument.Load("list.xml");
var result = doc.XPathSelectElements("List/Item");
if(result != null)
{
foreach (var item in result)
{
TestItem ti = new TestItem();
ti.Header = item.Attribute("Header").Value;
ti.Width = Convert.ToInt32(item.Attribute("Width").Value);
list.Add(ti);
}
}
return list;
} /// <summary>
/// 从数据库获取数据
/// </summary>
/// <returns></returns>
private DataTable GetData()
{
DataTable dt = new DataTable();
string sql = "";
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
try
{
SqlConnection con = new SqlConnection(conStr);
sql = "select * from Student";
SqlDataAdapter sda = new SqlDataAdapter(sql,con);
sda.Fill(dt);
return dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return dt;
}
}
} struct TestItem
{
public string Header { get; set; } public int Width { get; set; }
} public class AgeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value + "岁";
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} }
运行效果