潘鹏整理WPF(8)列表控件ListBox&&ComboBox

列表控件

  • 继承Control.ItemControl,意味着可以放任意元素
  • ListBox和ComboBox的区别在于:
    • ListBox可以多选,ComboBox只能单选
    • 表现形式区别,ComboBox是个下拉列表的样子

ListBox

这里写图片描述

<ListBox  Name="Lb" Height="158" Margin="93,58,0,0"  Width="277" SelectionChanged="Selector_OnSelectionChanged">
   <StackPanel Orientation="Horizontal">
       <CheckBox Height="20"></CheckBox>
       <Image Source="1.png" Width="43" Height="46"></Image>
       <Label Content="TXT的图标"></Label>
   </StackPanel>
   <StackPanel Orientation="Horizontal">
       <CheckBox Height="20"></CheckBox>
       <Image Source="2.png" Width="43" Height="46"></Image>
       <Label Content="This is Camera"></Label>
   </StackPanel>
   <StackPanel Orientation="Horizontal">
       <CheckBox Height="20"></CheckBox>
       <Image Source="8.png" Width="43" Height="46"></Image>
       <Label Content="文件夹"></Label>
   </StackPanel>
</ListBox>

<TextBlock Name="Tb" HorizontalAlignment="Left" Margin="167,79,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="277"/>

private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
     if(this.Lb.SelectedItems == null)return;
     this.Tb.Text = "选择的是第" + Lb.SelectedIndex + ":" +  ((CheckBox)((StackPanel)Lb.SelectedItems[0]).Children[0]).IsChecked;
}

选中列表,触发SelectionChanged事件
获取选中的内容,需要根据内容是什么类型来强转,例如上面强转StackPanel、CheckBox两次

ComboBox

这里写图片描述

这里写图片描述
comboBox和ListBox相比,没有特别的属性,这里是用绑定写的,就不上代码了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值