1.设置ListBox的SelectionMode属性为"Multiple"
2.遍历ListBox属性,这是个List,里面存放的都是选中了的行的对象
代码如下:
界面代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox SelectionMode="Multiple" Height="85" HorizontalAlignment="Left" Margin="96,22,0,0" Name="listBox1" VerticalAlignment="Top" Width="179">
<ListBoxItem Content="1" />
<ListBoxItem Content="2" />
<ListBoxItem Content="3" />
<ListBoxItem Content="4" />
</ListBox>
<Button Content="拼接选中内容" Height="23" HorizontalAlignment="Left" Margin="96,129,0,0" Name="button1" VerticalAlignment="Top" Width="93" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="96,173,0,0" Name="textBox1" VerticalAlignment="Top" Width="179" />
</Grid>
</Window>
C#代码:我在鼠标点击按钮的时候,对LIstBox的选中行进行了遍历,然后拼接其中的文本
private void button1_Click(object sender, RoutedEventArgs e)
{
int i = 0;
ListBoxItem item = null;
string pjStr = "";
while (i < listBox1.SelectedItems.Count)
{
item = listBox1.SelectedItems as ListBoxItem;
pjStr = pjStr + item.Content;
i++;
}
textBox1.Text = pjStr;
}
效果如下:
C:\Users\lenovo\Desktop\1.PNG |