ListBox
SelectionMode
- Multiple: 通过单击项选择和取消
- Extended: 通过Ctrl或Shift选择某个范围
ListBoxItem
列表项可以被隐式创建,以下两种格式均正确。但是如果通过SelectedItem获取选中项时,下列代码中,第一个获取的是ListBoxItem对象,第二个获取的是StackPanel对象。
//ListBox.SelectedItem对象为ListBoxItem
<ListBox Grid.Row="0">
<ListBoxItem>Green</ListBoxItem>
<ListBoxItem>Red</ListBoxItem>
</ListBox>
//ListBox.SelectedItem对象为StackPanel
<ListBox Grid.Row="1">
<StackPanel>
<Label>Blue</Label>
</StackPanel>
<StackPanel>
<Label>Black</Label>
</StackPanel>
</ListBox>
ItemContainerStyle
通过该属性设置列表项的样式
<ListBox Grid.Row="0" Name="listBox1" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="ListBoxItem.Background" Value="LightSteelBlue"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>Green</ListBoxItem>
<ListBoxItem>Red</ListBoxItem>
</ListBox>