2021-08-20 WPF控件专题 ListView控件详解
Posted 微软MVP Eleven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-08-20 WPF控件专题 ListView控件详解相关的知识,希望对你有一定的参考价值。
1.ListView控件介绍
Listbox ComboBox TabControl ItemsControl 条目控件
Winform ListView 5View
WPF ListView :用于显示数据项的列表(DataGrid ) ListBox
组织数据的结构和样式----显示数据
View : GridView:ListView 控件的以 列 形式显示数据项的 “视图模式”
GridView属性:Columns ColumnHeaderTemplate AllowsColumnReorder(true)
ColumnHeaderToolTip
ItemsSource:数据源 IEnumerable List
列:GridViewColumn 显示数据的列
属性:Header 列标题 Width 宽度 HeaderTemplate 显示列标题内容的模板
CellTemplate 显示列单元格的内容的模板
DisplayMemberBinding 要绑定到此列的数据项
将数据显示到ListView — 数据绑定(ItemsSource 后台代码指定 Xaml代码中设置)
定义GridView 列集合
UserInfos 显示到ListView
2.具体案例
<Window x:Class="WpfAppTest.ListViewWindow"
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:WpfAppTest"
mc:Ignorable="d"
Title="ListViewWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<ListView Name="lvList" HorizontalAlignment="Left" Height="228" Margin="88,133,0,0" VerticalAlignment="Top" ItemsSource="{Binding}" >
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox Name="chkAll" Content="全选" />
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chk" Tag="{Binding UserId}" IsChecked="{Binding ElementName=chkAll,Path=IsChecked,Mode=OneWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="编号" DisplayMemberBinding="{Binding UserId}" Width="50">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<Label Content="{Binding}" Foreground="Red" BorderThickness="1" BorderBrush="YellowGreen" />
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
<GridViewColumn Header="账号" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding UserName}" Foreground="YellowGreen" HorizontalAlignment="Left" Width="60" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="状态" DisplayMemberBinding="{Binding UserState}" Width="50"/>
<GridViewColumn Header="部门编号" DisplayMemberBinding="{Binding DeptId}" Width="60"/>
<GridViewColumn Header="年龄" DisplayMemberBinding="{Binding UserAge}" Width="50"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
/// <summary>
/// ListViewWindow.xaml 的交互逻辑
/// </summary>
public partial class ListViewWindow : Window
{
public ListViewWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//代码设置ItemsSource
// lvList.ItemsSource = GetUserList();
lvList.DataContext = GetUserList();
}
/// <summary>
/// 获取用户信息列表
/// </summary>
/// <returns></returns>
private List<UserInfo> GetUserList()
{
List<UserInfo> list = new List<UserInfo>();
string sql = "select UserId,UserName,UserState,UserAge,DeptId from UserInfos where DeptId>0";
SqlDataReader dr = SqlHelper.ExecuteReader(sql, 1);
while(dr.Read())
{
UserInfo user = new UserInfo();
user.UserId = (int)dr["UserId"];
user.UserName = dr["UserName"].ToString();
user.UserState = (int)dr["UserState"];
user.UserAge = (int)dr["UserAge"];
user.DeptId = (int)dr["DeptId"];
list.Add(user);
}
dr.Close();
return list;
}
}
public class UserInfo
{
public int UserId { get; set; }
public string UserName { get; set; }
public int UserState { get; set; }
public int UserAge { get; set; }
public int DeptId { get; set; }
}
以上是关于2021-08-20 WPF控件专题 ListView控件详解的主要内容,如果未能解决你的问题,请参考以下文章
2021-08-13 WPF控件专题 ComboBox 控件详解
2021-08-19 WPF控件专题 TabControl 控件详解