XAML:
在控件中一次选择多个数据项: a b c d e 在控件中使用Ctrl一次选择多个数据项: 张三 李四 王五 赵六 孙七
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApplication1{ ////// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnShowSelectItems(object sender, RoutedEventArgs e) { string msg=string.Empty; Listlist = new List (); if (lbx.SelectedItems != null && lbx.SelectedItems.Count > 1) { //多选 foreach (ListBoxItem lbi in lbx.SelectedItems) { list.Add(lbi.Content.ToString()); } msg = string.Join(",", list); } else if (lbx.SelectedItems != null && lbx.SelectedItems.Count == 1) { //单选 msg = ((ListBoxItem)lbx.SelectedItem).Content.ToString(); } else { //没有选中任何项 } MessageBox.Show(msg); } }}
效果: