ListView的View是ViewBase类型,而GridView继承与ViewBase,所以可以将GridView赋值给ListView的View,ListView可以单独使用,GridView不可以单独使用,目前ViewBase只有GridView这一个派生类
<Window x:Class="WpfApp11.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:WpfApp11"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" >
<Grid>
<ListView x:Name="lstView">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" Width="100" DisplayMemberBinding="{Binding Id}"></GridViewColumn>
<GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
<GridViewColumn Header="Age" Width="100" DisplayMemberBinding="{Binding Age}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
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;
namespace WpfApp11
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
lstView.ItemsSource = GetUserInfo().DefaultView;
}
private DataTable GetUserInfo()
{
string conStr = "server=.;database=dbUser;user=sa;pwd=Server2012";
using (SqlConnection conn = new SqlConnection(conStr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("select * from tbUser", conn))
{
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds, "user");
return ds.Tables["user"];
}
}
}
}
}