WPF —— ListBox控件、GroupBox控件详解

1、ListBox 介绍

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

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

2 常用的属性

ItemTemplate 子项模版标签
DataTemplate 子项里面数据的模版

3 ListBox的实例

动态绑定数据:使用cs代码添加子选项.必须添加ItemsSource="{Binding}"语法
静态绑定数据:直接使用标签添加子选项

<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
   
    <ListBox Width="200"
             Height="400"
             Background="Pink"
             ItemsSource="{Binding}"
             Name="l2">
        <!--演示静态绑定数据-->
        <ListBoxItem>
            <Button>武庚纪</Button> 
        </ListBoxItem>
        <ListBoxItem>斗罗大陆</ListBoxItem>
        <ListBoxItem>星辰变</ListBoxItem>
        
    </ListBox>
    <!--演示的是动态绑定数据 ItemsSource="{Binding}"-->
    <!--演示自定义模版子项-->
    <ListBox Width="200"
             Height="400"
             Name="l1"
             FontSize="20"
             SelectionMode="Extended"
             ItemsSource="{Binding}"
             Margin="100,0,0,0"
             Background="Teal">
        <!--ItemTemplate 子项模版标签-->
        <!--DataTemplate 子项里面数据的模版-->
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding}" Background="red"></Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
          
    </ListBox>
        
     <!--演示绑定数据是对象结构-->
    <ListBox Name="l3" Width="200" Margin="100,0,0,0" Background="Beige" 
             ItemsSource="{Binding}">
        
        
    </ListBox>

</StackPanel>

 

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        List<string> list = new List<string>();
        list.Add("张三");
        list.Add("李四");
        list.Add("王五");
        list.Add("马六");
        this.l1.DataContext = list;
       
        List<DaJia> list2 = new List<DaJia>();
        list2.Add(new DaJia() { 
            Name = "淀粉肠",
            Description = "是骨头捣碎泥"
        });
        list2.Add(new DaJia()
        {
            Name = "梅菜扣肉",
            Description = "淋巴结肉"
        });
        this.l3.DataContext = list2;
        this.l3.DisplayMemberPath = "Description"; //指定显示属性的路径
     
    }
}
public class DaJia {
    public string Name { get; set; } // 打假名称
    public string Description { get; set; } // 打假描述

}

 

相关推荐

  1. WPF —— ListBoxGroupBox详解

    2024-03-22 09:34:06       37 阅读
  2. 【WPF应用42】WPF中的 GroupBox 详解

    2024-03-22 09:34:06       38 阅读
  3. WPF —— TextBox 详解

    2024-03-22 09:34:06       42 阅读
  4. WPF —— ComboBox详解

    2024-03-22 09:34:06       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-22 09:34:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-22 09:34:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-22 09:34:06       82 阅读
  4. Python语言-面向对象

    2024-03-22 09:34:06       91 阅读

热门阅读

  1. How to install Miniconda on ubuntu 22.04

    2024-03-22 09:34:06       37 阅读
  2. 06 分页

    2024-03-22 09:34:06       37 阅读
  3. LINQ常用扩展方法、委托、Lambda、yield

    2024-03-22 09:34:06       35 阅读
  4. JPA使用CriteriaQuery实现动态分组查询

    2024-03-22 09:34:06       44 阅读
  5. 浅谈业务逻辑漏洞

    2024-03-22 09:34:06       40 阅读
  6. Devin 40: 人工智能与未来的融合

    2024-03-22 09:34:06       44 阅读
  7. 构建Pytorch虚拟环境教程

    2024-03-22 09:34:06       42 阅读
  8. ROS git使用

    2024-03-22 09:34:06       32 阅读
  9. RHCE 第二章 时间服务器

    2024-03-22 09:34:06       43 阅读
  10. docker基础(五)之docker run(第二弹)

    2024-03-22 09:34:06       42 阅读
  11. P1005 [NOIP2007 提高组] 矩阵取数游戏

    2024-03-22 09:34:06       40 阅读