您的位置:首页 > 其它

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

2015-09-22 12:21 453 查看

列表控件

继承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相比,没有特别的属性,这里是用绑定写的,就不上代码了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: