ListView
表示用于显示数据项列表的控件。
ListView 为 ItemsControl ,这意味着它可以包含任何类型的对象的集合 (例如字符串、图像或面板) 。 有关更多信息,请参见 ItemsControl 类。
ListView中数据项的表示方式由其查看模式定义,该模式由View属性指定。Windows Presentation Foundation(WPF)提供了GridView视图模式,该模式将ListView数据项内容划分为多个列。GridView及其相关类的属性和方法将样式化并指定列的内容。
您还可以通过创建派生自ViewBase的类来定义自定义视图。有关更多信息,请参见如何:为ListView创建自定义视图模式。
如果您为ListView控件定义了自定义ItemContainerStyle,并且还定义了ItemTemplate,则必须在ItemContainerStyle中包括ContentPresenter。必须使用ContentPresenter才能使ItemTemplate正常工作。
为了避免GridView视图模式下的对齐问题,请不要在ListView的ItemContainerStyle中添加影响项目宽度的内容或设置属性。为防止对齐问题,请使用为GridView视图模式定义的属性和模板。这包括为在GridView视图模式下使用的类定义的属性和模板,例如GridViewColumn和GridViewColumnHeader。
两个ListView控件不能共享同一视图。若要指定可以多次使用的视图,请使用模板或样式。有关定义为可重用资源的GridView和其他自定义视图的示例,请参见具有多个视图示例的ListView。
不要对使用GridView显示的ListView内容使用HorizontalContentAlignment和VerticalContentAlignment属性。要在GridView的列中指定内容的对齐方式,请定义一个CellTemplate。
显示大量项目可能会导致性能问题。有关更多信息,请参见优化性能:控件。
自定义ListView控件
要将相同的属性设置应用于多个ListView控件,请使用Style属性。您可以修改默认的ControlTemplate以使控件具有唯一的外观。有关创建ControlTemplate的更多信息,请参见通过创建ControlTemplate自定义现有控件的外观。若要查看特定于ListView的零件和状态,请参见ListView样式和模板。
此控件的依赖项属性可以由控件的默认样式设置。如果使用默认样式设置属性,则当控件出现在应用程序中时,该属性可能会从其默认值更改。默认样式由运行应用程序时使用的桌面主题确定。有关更多信息,请参见默认WPF主题。
Note
设置视觉属性仅在该属性同时存在于控件的默认模板中并且通过使用TemplateBinding进行设置时才有效。您可以在通过创建ControlTemplate定制现有控件的外观的“更改控件的视觉结构”部分中找到视觉属性列表。
名称 | 备注 | 权限 |
---|---|---|
标识 View 依赖项属性。 | public static readonly |
名称 | 备注 | 权限 |
---|---|---|
获取或设置一个对象,用于定义如何在 ListView 控件中设置数据样式和组织数据。 | get; set; |
名称 | 备注 | 权限 |
---|---|---|
移除显示为 ListViewItem 的对象的所有模板、样式和绑定。 | protected | |
创建并返回一个新的 ListViewItem 容器。 | protected | |
确定对象是否为 ListViewItem。 | protected | |
定义 AutomationPeer 控件的 ListView。 | protected | |
响应 OnItemsChanged(NotifyCollectionChangedEventArgs)。 | protected | |
为 ListViewItem 设置样式、模板和绑定。 | protected |
ListViewItem
表示 ListView 控件中的一个项。
ListViewItem是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。
ListView通常通过ListViewItem 设置 ItemsSource 属性或 Items 属性来指定其控件的内容。
为 ListViewItem 类型定义的模板和样式,用于指定在 ListView 中显示内容。
自定义 ListViewItem 控件
若要对多个 ListViewItem 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 ListViewItem 的部分和状态,请参阅 ListView 样式和模板。
此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题。
Note
只有视觉对象属性已存在于TemplateBinding 控件的默认模板中并且已使用 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。
基本功能和ListBox相似,多了一个View属性,如上图。
XAML范例
<Window x:Class="ListViewDemo.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:ListViewDemo"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="600">
<StackPanel x:Name="myStackPanel">
<ListView SelectionMode="Extended" Height="200" SelectionChanged="OnSelectionChanged" >
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
<GridViewColumn Header="First Name" Width="100"/>
<GridViewColumn Width="100">
<GridViewColumnHeader>Last Name
</GridViewColumnHeader>
</GridViewColumn>
<GridViewColumn Header="Employee No." Width="100"/>
</GridView>
</ListView.View>
<ListViewItem Content="ListViewItem0" />
<ListViewItem Content="ListViewItem1" IsSelected="True"/>
<ListViewItem Content="ListViewItem2"/>
<ListViewItem Content="ListViewItem3" IsSelected="True"/>
<ListViewItem Content="ListViewItem4"/>
<ListViewItem Content="ListViewItem5"/>
</ListView>
<TextBlock Height="200">
</TextBlock>
<Button Content="全选" Click="OnListViewSelectAll"/>
<Button Content="取消全选" Click="OnListViewUnselectAll"/>
</StackPanel>
</Window>
using System;
using System.Collections;
using System.Collections.Generic;
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 ListViewDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnListViewSelectAll(object sender, RoutedEventArgs e)
{
foreach (var item in ((sender as Button).Parent as StackPanel).Children)
{
if (item is ListBox) (item as ListBox).SelectAll();
}
}
private void OnListViewUnselectAll(object sender, RoutedEventArgs e)
{
foreach (var item in ((sender as Button).Parent as StackPanel).Children)
{
if (item is ListBox) (item as ListBox).UnselectAll();
}
}
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string s = String.Empty;
foreach (var item0 in (sender as ListBox).SelectedItems)
{
s += (item0 as ListBoxItem).Content + "\n";
}
foreach (var item in ((sender as ListBox).Parent as StackPanel).Children)
{
if (item is TextBlock) (item as TextBlock).Text = s;
}
}
}
}
C#范例
using System;
using System.Collections;
using System.Collections.Generic;
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 ListViewDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = true;
myGridView.ColumnHeaderToolTip = "Employee Information";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.Header = "FirstName";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.Header = "Last Name";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.Header = "Employee No.";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
ListView listview = new ListView();
listview.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
listview.Height = 200;
listview.SelectionChanged += OnSelectionChanged;
for (int i = 0; i < 8; i++)
{
listview.Items.Add(new ListViewItem { Content = "ListViewItem" + i });
}
(listview.Items[1] as ListViewItem).IsSelected = true;
(listview.Items[3] as ListViewItem).IsSelected = true;
(listview.Items[6] as ListViewItem).IsSelected = true;
TextBlock textBlock = new TextBlock { Height = 200 };
Button button1 = new Button { Content = "全选" };
button1.Click += OnListViewSelectAll;
Button button2 = new Button { Content = "取消全选" };
button2.Click += OnListViewUnselectAll;
StackPanel mystackPanel = new StackPanel();
mystackPanel.Children.Add(listview);
mystackPanel.Children.Add(textBlock);
mystackPanel.Children.Add(button1);
mystackPanel.Children.Add(button2);
this.Content=mystackPanel;
foreach (var item in (this.Content as StackPanel).Children)
{
if (item is ListView) (item as ListView).View = myGridView;
}
}
private void OnListViewSelectAll(object sender, RoutedEventArgs e)
{
foreach (var item in ((sender as Button).Parent as StackPanel).Children)
{
if (item is ListBox) (item as ListBox).SelectAll();
}
}
private void OnListViewUnselectAll(object sender, RoutedEventArgs e)
{
foreach (var item in ((sender as Button).Parent as StackPanel).Children)
{
if (item is ListBox) (item as ListBox).UnselectAll();
}
}
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string s = String.Empty;
foreach (var item0 in (sender as ListBox).SelectedItems)
{
s += (item0 as ListBoxItem).Content + "\n";
}
if((sender as ListBox).Parent !=null)
{
foreach (var item in ((sender as ListBox).Parent as StackPanel).Children)
{
if (item is TextBlock) (item as TextBlock).Text = s;
}
}
}
}
}