• WPF 控件专题 ListView 控件详解


    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来实现。

    1. <Window.Resources>
    2. <local:Students x:Key="myStudents"/>
    3. Window.Resources>
    4. <ListView ItemsSource="{Binding Source={StaticResource myStudents} }" HorizontalAlignment="Center" Width="320" >
    5. <ListView.View>
    6. <GridView>
    7. <GridViewColumn Width="70" Header="年龄" DisplayMemberBinding="{Binding Path=Name}"/>
    8. <GridViewColumn Width="70" Header="性别" DisplayMemberBinding="{Binding Path=Sex}" />
    9. <GridViewColumn Width="70" Header="年龄" DisplayMemberBinding="{Binding Path=Age}"/>
    10. <GridViewColumn Width="90" x:Name="column" Header="城市">
    11. <GridViewColumn.CellTemplate>
    12. <DataTemplate>
    13. <TextBlock Margin="-6 0" Text="{Binding Path=City}" TextAlignment="Center" MinWidth="{Binding Path=ActualWidth,ElementName=column}"/>
    14. DataTemplate>
    15. GridViewColumn.CellTemplate>
    16. GridViewColumn>
    17. GridView>
    18. ListView.View>
    19. ListView>

        后台定义类数据

    1. public partial class MainWindow : Window
    2. {
    3. public MainWindow()
    4. {
    5. InitializeComponent();
    6. this.DataContext = this;
    7. }
    8. public class Student
    9. {
    10. public string Name { get; set; }
    11. public string Sex { get; set; }
    12. public int Age { get; set; }
    13. public string City { get; set; }
    14. }
    15. public class Students : ObservableCollection<Student>
    16. {
    17. public Students()
    18. {
    19. Add(new Student() { Name = "张三", Age = 19, City = "北海", Sex = "男" });
    20. Add(new Student() { Name = "李四", Age = 29, City = "深圳", Sex = "男" });
    21. Add(new Student() { Name = "王五", Age = 39, City = "山东", Sex = "男" });
    22. Add(new Student() { Name = "张龙", Age = 49, City = "福建", Sex = "男" });
    23. Add(new Student() { Name = "赵虎", Age = 59, City = "浙江", Sex = "男" });
    24. Add(new Student() { Name = "王朝", Age = 69, City = "新疆维吾尔自治区", Sex = "男" });
    25. Add(new Student() { Name = "马汉", Age = 79, City = "广西壮族自治区", Sex = "男" });
    26. }
    27. }
    28. }

    **************************************************************************************************************

    4、效果图

    **************************************************************************************************************

    5、总结和扩展

        样式改写示例:WPF 控件专题 ListView 样式(斑马线)

    **************************************************************************************************************

  • 相关阅读:
    Unity技术手册-UGUI零基础详细教程-Text文本(打字、阴影、渐变)
    【.NET】控制台应用程序的各种交互玩法
    成都东部新区文化旅游体育局莅临国际数字影像产业园参观入驻企业,共促政产交流“零距离”
    【Android 四大组件之Content Provider】一文吃透Content Provider 内容提供者
    Kubernetes 系统化学习之 资源清单篇(三)
    HTML静态网页作业html+css+javascript+jquery水果商城7页
    springboot读取配置文件自定义参数和自定义配置文件参数
    JAVA智慧工地管理系统源码基于微服务
    SAP ABAP OData 服务如何支持 $filter (过滤)操作试读版
    MongoDB权限验证开启与mongoose数据库配置
  • 原文地址:https://blog.csdn.net/BYH371256/article/details/125553855