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; } // 打假描述

}

 

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值