• WPF 控件专题 ListBox 控件详解


    1、ListBox 介绍

        ListBox 列表控件。

        ListBox 是一个 ItemsControl,这意味着它可以包含任何类型的对象的集合 (,例如字符串、图像或面板) 。

        一个 ListBox 中的多个项是可见的,与仅 ComboBox具有所选项可见的项不同,除非 IsDropDownOpen 属性为 true。 该 SelectionMode 属性确定一次是否可以选择多个项 ListBox 。

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

    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 的内容的集合。
        
        ItemsSource:获取或设置用于生成 ItemsControl 的内容的集合。

        SelectedIndex:获取或设置当前选择中第一项的索引,如果选择为空,则返回负一(-1)。

        SelectedItem:获取或设置当前选择中的第一项,或者,如果选择为空,则返回 null。

        SelectedItems:获取当前选定的项。

        SelectedValue:获取或设置通过使用 SelectedItem 而获取的 SelectedValuePath 的值。

        SelectedValuePath:获取或设置用于从 SelectedValue 获取 SelectedItem 的路径。

        SelectionMode:获取或设置 ListBox 的选择行为。

        SnapsToDevicePixels:获取或设置一个值,该值确定在呈现过程中,此元素的呈现是否应使用特定于设备的像素设置。

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

    3、具体示例代码

        XAML代码

    1. <StackPanel>
    2. <TextBox Name="tb" Width="180" Height="30">TextBox>
    3. <ListBox Name="lb" Width="140" SelectionChanged="PrintText" SelectionMode="Single" Margin="10">
    4. <ListBoxItem>Item 1ListBoxItem>
    5. <ListBoxItem>Item 2ListBoxItem>
    6. <ListBoxItem>Item 3ListBoxItem>
    7. <ListBoxItem>Item 4ListBoxItem>
    8. <ListBoxItem>Item 5ListBoxItem>
    9. <ListBoxItem>Item 6ListBoxItem>
    10. <ListBoxItem>Item 7ListBoxItem>
    11. <ListBoxItem>Item 8ListBoxItem>
    12. <ListBoxItem>Item 9ListBoxItem>
    13. <ListBoxItem>Item 10ListBoxItem>
    14. ListBox>
    15. StackPanel>

        后端代码

    1. private void PrintText(object sender, SelectionChangedEventArgs e)
    2. {
    3. ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    4. tb.Text = " You selected " + lbi.Content.ToString() + ".";
    5. }

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

    4、效果图

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

    5、自定义样式

    1. <Style x:Key="listBoxItemStyle" TargetType="{x:Type ListBoxItem}">
    2. <Setter Property="Template">
    3. <Setter.Value>
    4. <ControlTemplate TargetType="{x:Type ListBoxItem}">
    5. <Border x:Name="IconBorder" Background="#555a64" CornerRadius="4" BorderThickness="0" Margin="5 5">
    6. <ContentPresenter />
    7. Border>
    8. <ControlTemplate.Triggers>
    9. <Trigger Property="IsSelected" Value="true">
    10. <Setter TargetName="IconBorder" Property="BitmapEffect">
    11. <Setter.Value>
    12. <OuterGlowBitmapEffect GlowColor="Transparent" GlowSize="5" />
    13. Setter.Value>
    14. Setter>
    15. Trigger>
    16. ControlTemplate.Triggers>
    17. ControlTemplate>
    18. Setter.Value>
    19. Setter>
    20. Style>
    1. <ListBox x:Name="MyListBox" Background="#2d323c" Width="200" Margin="20"
    2. ItemContainerStyle="{StaticResource listBoxItemStyle}" FocusVisualStyle="{x:Null}" ItemsSource="{Binding ListBoxDatas}">
    3. <ListBox.Template>
    4. <ControlTemplate>
    5. <StackPanel Background="White" IsItemsHost="True">StackPanel>
    6. ControlTemplate>
    7. ListBox.Template>
    8. <ListBox.ItemTemplate>
    9. <DataTemplate>
    10. <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
    11. <CheckBox IsChecked="{Binding IsChecked}" Height="20" Width="20" VerticalAlignment="Center" Margin="10,5"/>
    12. <Ellipse Height="14" Width="14" Fill="{Binding Color}" VerticalAlignment="Center" Margin="5"/>
    13. <TextBlock Text="{Binding NameText}" VerticalAlignment="Center" Margin="5" FontSize="16"/>
    14. StackPanel>
    15. DataTemplate>
    16. ListBox.ItemTemplate>
    17. ListBox>
    1. public partial class MainWindow : Window, System.ComponentModel.INotifyPropertyChanged
    2. {
    3. public MainWindow()
    4. {
    5. InitializeComponent();
    6. this.DataContext = this;
    7. ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Red, IsChecked = false, NameText = "张涛涛1" });
    8. ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Green, IsChecked = true, NameText = "张涛涛2" });
    9. ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Blue, IsChecked = false, NameText = "张涛涛3" });
    10. ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Orange, IsChecked = true, NameText = "张涛涛4" });
    11. ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Yellow, IsChecked = false, NameText = "张涛涛5" });
    12. ListBoxDatas.Add(new ListBoxData() { Color = Brushes.YellowGreen, IsChecked = true, NameText = "张涛涛6" });
    13. }
    14. private ListlistBoxDatas = new List();
    15. public List ListBoxDatas
    16. {
    17. get { return listBoxDatas; }
    18. set { listBoxDatas = value; OnPropertyChanged("ListBoxDatas"); }
    19. }
    20. public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    21. protected void OnPropertyChanged(string propertyName)
    22. {
    23. if (PropertyChanged != null)
    24. {
    25. PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    26. }
    27. }
    28. }
    29. public class ListBoxData
    30. {
    31. public bool IsChecked { get; set; }
    32. public Brush Color { get; set; }
    33. public string NameText { get; set; }
    34. }

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

    效果图

     通过Template改变列表的布局,如使用WrapPanel进行布局;或者ItemsPanel也可

    1. <ListBox.Template>
    2. <ControlTemplate>
    3. <WrapPanel Background="#2d323c" IsItemsHost="True"/>
    4. ControlTemplate>
    5. ListBox.Template>

    效果图

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

    6、总结和扩展

        IsItemsHost=true表示子元素将显示在此容器中。

        第5点,演示了自定义的样式和Data绑定功能。

        该 SelectionMode 属性确定用户可以一次选择的项数。 可以将属性设置为 Single (默认) , Multiple或 Extended。 下表描述了这些枚举值的行为。
            Single:用户一次只能选择一项。
            Multiple:用户可以选择多个项而无需按下修改键。
            Extended:用户可以通过按住 Ctrl 键并单击项来选择多个连续项,同时按住 SHIFT 键或非连续项。

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

  • 相关阅读:
    file-storage-sdk项目开发中的踩坑记录
    【图神经网络】基于图的生成模型
    代码随想录Day25 回溯算法 LeetCode T51 N皇后问题
    AI虚拟人物 数字人直播,不用出镜,不用露脸的直播方式(附教程 软件)
    go 语言 mage 安装踩坑
    使用ssh上传数据到阿里云ESC云服务上
    MySQL BufferPool缓存与Redo日志是如何提升事务性能的
    六、流量监管、流量整形
    MySQL最新版8.1.0安装配置教程
    Vue3 <script setup lang=“ts“> 使用指南
  • 原文地址:https://blog.csdn.net/BYH371256/article/details/125513957