WPF
中的 ListView
是一个强大的控件,可以显示列表数据。它继承自 ListBox
,并支持更多的功能,包括使用不同的 View
模板(例如 GridView
)。ListView
可以用于创建复杂的表格结构,并且通过数据绑定功能,能够轻松地与数据模型交互。下面是 ListView
的详细使用教程。
1. 基本 ListView
控件
最简单的 ListView
用法就是显示一组项目。你可以直接在 XAML
中定义它的项:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListView Example" Height="350" Width="525">
<Grid>
<ListView>
<ListViewItem Content="Item 1" />
<ListViewItem Content="Item 2" />
<ListViewItem Content="Item 3" />
</ListView>
</Grid>
</Window>
2. 使用 GridView
显示列
ListView
控件的一个强大功能是它能够结合 GridView
来显示具有列的列表。每一列都可以显示不同的属性,类似于 DataGrid
。
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" />
</GridView>
</ListView.View>
</ListView>
你可以通过 GridViewColumn
的 Header
属性来设置列名,通过 DisplayMemberBinding
来绑定数据源的属性。
3. 绑定数据源
在实际应用中,ListView
经常与数据绑定结合使用。可以将数据源设置为 ObservableCollection
或其他集合类,ListView
会自动显示数据。以下是通过 ObservableCollection
绑定数据的示例:
XAML 代码:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListView Example" Height="350" Width="525">
<Grid>
<ListView ItemsSource="{Binding People}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
C# 代码:
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
public ObservableCollection<Person> People { get; set; }
public MainWindow()
{
InitializeComponent();
// 设置数据源
People = new ObservableCollection<Person>
{
new Person { Name = "Alvin", Age = 30, Country = "China" },
new Person { Name = "Tom", Age = 25, Country = "USA" },
new Person { Name = "Lisa", Age = 28, Country = "UK" }
};
// 将 DataContext 绑定到当前窗口
DataContext = this;
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}
}
在此例中,ListView
通过 ItemsSource="{Binding People}"
绑定到 ObservableCollection
。DisplayMemberBinding
指定了要显示的数据属性。
4. 自定义 ItemTemplate
如果你需要更复杂的布局,可以通过自定义 ItemTemplate
来控制每一项的显示方式。使用 DataTemplate
可以自定义 ListViewItem
的外观。
XAML 代码:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListView Example" Height="350" Width="525">
<Grid>
<ListView ItemsSource="{Binding People}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Width="100" />
<TextBlock Text="{Binding Age}" Width="50" />
<TextBlock Text="{Binding Country}" Width="100" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
在这里,我们通过 StackPanel
来自定义每一行的数据排列,使用 TextBlock
来显示每个属性。
5. 处理选中项
ListView
提供了 SelectedItem
属性,用于获取用户选中的项。你可以在代码中访问该属性来进行逻辑处理。
XAML 代码:
<ListView ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}">
<!-- GridView 定义 -->
</ListView>
C# 代码:
public class MainWindowViewModel : INotifyPropertyChanged
{
private Person _selectedPerson;
public ObservableCollection<Person> People { get; set; }
public Person SelectedPerson
{
get { return _selectedPerson; }
set
{
_selectedPerson = value;
OnPropertyChanged(nameof(SelectedPerson));
}
}
// INotifyPropertyChanged 实现
}
6. 多选功能
ListView
支持多选功能,可以通过设置 SelectionMode
属性来实现。
Single
:单选(默认值)Multiple
:多选Extended
:多选,可以通过 Shift 或 Ctrl 键选择多个项。
<ListView ItemsSource="{Binding People}" SelectionMode="Multiple">
<!-- GridView 定义 -->
</ListView>
你可以在代码中通过 SelectedItems
属性获取所有选中的项。
7. 排序功能
通过 CollectionViewSource
和 SortDescription
,可以对 ListView
数据进行排序。以下是按 Name
属性进行排序的示例:
引入: xmlns:scm=“clr-namespace:System.ComponentModel;assembly=WindowsBase”
<Window.Resources>
<CollectionViewSource x:Key="PeopleView" Source="{Binding People}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Name" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Source={StaticResource PeopleView}}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
在这里,通过 CollectionViewSource
资源为 ListView
提供了排序功能。
8. 分组功能
ListView
还支持按条件对数据进行分组,可以通过 CollectionViewSource
和 GroupDescription
来实现。
<Window.Resources>
<CollectionViewSource x:Key="PeopleView" Source="{Binding People}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Country" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Source={StaticResource PeopleView}}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
通过设置 PropertyGroupDescription
,数据将根据 Country
属性进行分组显示。
9. 处理 ListView
事件
ListView
提供了多种事件来处理用户交互,例如 SelectionChanged
事件。当用户选择了不同的项时,可以触发该事件:
XAML 代码:
<ListView ItemsSource="{Binding People}" SelectionChanged="ListView_SelectionChanged">
<!-- GridView 定义 -->
</ListView>
C# 代码:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listView = sender as ListView;
var selectedPerson = listView.SelectedItem as Person;
if (selectedPerson != null)
{
MessageBox.Show($"
Selected: {selectedPerson.Name}");
}
}
总结
WPF ListView
是一个非常灵活且强大的控件,适用于显示简单到复杂的列表数据。你可以使用 GridView
显示列数据,通过数据绑定与后台数据模型交互,自定义项模板,并实现排序、分组和多选等高级功能。