1、ListView 介绍
ListView 表示用于显示数据项列表的控件。常和GridView一起使用。
GridView 表示 ListView 控件的以列形式显示数据项的视图模式。
ListView 是一个 ItemsControl,这意味着它可以包含任何类型 (的对象集合,例如字符串、图像或面板) 。
**************************************************************************************************************
2、常用属性介绍
FontFamily:字体系列; FontSize:字体大小; FontStretch:字体在屏幕上紧缩或加宽的程度;FontWeight:字体粗细;
Background:背景; BorderBrush:边框颜色; BorderThickness:边框宽度; Foreground:前景色;
Width/Height:宽度/高度; Name:元素标识名称; IsEnabled:使能,是否可用; Margin:外边距;
Opacity:透明度; Visibility:可见性; IsVisible:是否可见; FlowDirection:其子元素的流动方向;
LayoutTransform:在执行布局时应该应用于此元素的图形转换方式。 RenderTransform:元素的呈现位置的转换信息;
RenderTransformOrigin:由RenderTransform声明的任何可能呈现转换的中心点,相对于元素的边界。
HorizontalAlignment/VerticalAlignment:在父元素中组合此元素时所应用的水平对齐特征/垂直对齐特征。
HorizontalContentAlignment/VerticalContentAlignment:控件内容的水平对齐方式/垂直对齐方式。
Items:获取用于生成 ItemsControl 的内容的集合。ItemTemplate:获取或设置用来显示每个项的 DataTemplate。
ItemsPanel:获取或设置模板,该模板定义对项的布局进行控制的面板。
ItemsSource:获取或设置用于生成 ItemsControl 的内容的集合。
SelectedIndex:获取或设置当前选择中第一项的索引,如果选择为空,则返回负一(-1)。
SelectedItem:获取或设置当前选择中的第一项,或者,如果选择为空,则返回 null。
SelectedItems:获取当前选定的项。SelectionMode:获取或设置 ListBox 的选择行为。
SelectedValue:获取或设置通过使用 SelectedItem 而获取的 SelectedValuePath 的值。
SelectedValuePath:获取或设置用于从 SelectedValue 获取 SelectedItem 的路径。
SelectionBoxItem:获取在选择框中显示的项。SelectionBoxItemTemplate:获取选择框内容的项模板。
SnapsToDevicePixels:获取或设置一个值,该值确定在呈现过程中,此元素的呈现是否应使用特定于设备的像素设置。
**************************************************************************************************************
3、具体示例代码
XAML中ListView绑定静态资源,和显示的内容;GridViewColumn显示的内容是居左的,没有直接的属性进行设置,但是可以通过改写CellTemplate的DataTemplate来实现。
- <Window.Resources>
- <local:Students x:Key="myStudents"/>
- Window.Resources>
-
- <ListView ItemsSource="{Binding Source={StaticResource myStudents} }" HorizontalAlignment="Center" Width="320" >
- <ListView.View>
- <GridView>
- <GridViewColumn Width="70" Header="年龄" DisplayMemberBinding="{Binding Path=Name}"/>
- <GridViewColumn Width="70" Header="性别" DisplayMemberBinding="{Binding Path=Sex}" />
- <GridViewColumn Width="70" Header="年龄" DisplayMemberBinding="{Binding Path=Age}"/>
- <GridViewColumn Width="90" x:Name="column" Header="城市">
-
- <GridViewColumn.CellTemplate>
- <DataTemplate>
- <TextBlock Margin="-6 0" Text="{Binding Path=City}" TextAlignment="Center" MinWidth="{Binding Path=ActualWidth,ElementName=column}"/>
- DataTemplate>
- GridViewColumn.CellTemplate>
- GridViewColumn>
- GridView>
- ListView.View>
- ListView>
后台定义类数据
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- this.DataContext = this;
- }
-
- public class Student
- {
- public string Name { get; set; }
- public string Sex { get; set; }
- public int Age { get; set; }
- public string City { get; set; }
- }
- public class Students : ObservableCollection<Student>
- {
- public Students()
- {
- Add(new Student() { Name = "张三", Age = 19, City = "北海", Sex = "男" });
- Add(new Student() { Name = "李四", Age = 29, City = "深圳", Sex = "男" });
- Add(new Student() { Name = "王五", Age = 39, City = "山东", Sex = "男" });
- Add(new Student() { Name = "张龙", Age = 49, City = "福建", Sex = "男" });
- Add(new Student() { Name = "赵虎", Age = 59, City = "浙江", Sex = "男" });
- Add(new Student() { Name = "王朝", Age = 69, City = "新疆维吾尔自治区", Sex = "男" });
- Add(new Student() { Name = "马汉", Age = 79, City = "广西壮族自治区", Sex = "男" });
- }
- }
- }
**************************************************************************************************************
4、效果图

**************************************************************************************************************
5、总结和扩展
样式改写示例:WPF 控件专题 ListView 样式(斑马线)
**************************************************************************************************************