13. 項目を削除する
単一の項目を削除するにはItemsプロパティのRemoveAtメソッドを使用します。RemoveAtメソッドの引数には削除したい項目のインデックス番号を指定します。また、すべての項目を削除したい場合にはItemsプロパティのClearメソッドを使用します。
下記は、「選択項目の削除」および「全項目の削除」をする例です。

'[選択項目を削除]ボタンクリック時の処理
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
'選択項目がない場合は処理をしない
If ListBox1.SelectedItems.Count = 0 Then
Return
End If
'選択された項目を削除
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
'[全項目削除]ボタンクリック時の処理
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
'全項目を削除
ListBox1.Items.Clear()
End Sub
// [選択項目を削除]ボタンクリック時の処理
private void button1_Click(object sender, RoutedEventArgs e)
{
// 選択項目がない場合は処理をしない
if (listBox1.SelectedItems.Count == 0)
return;
// 選択された項目を削除
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
// [全項目削除]ボタンクリック時の処理
private void button2_Click(object sender, RoutedEventArgs e)
{
// 全項目を削除
listBox1.Items.Clear();
}
単一の項目を削除するにはRemoveAtメソッドを使用する。全項目を削除するにはClearメソッドを使用する。
14. ハイライトカラーを設定する
通常、項目が選択されると青色でハイライトされます。このハイライトカラーを変えるにはControlTemplateを変更します。WPFでは、ControlTemplateのTrigger要素とSetter要素で選択時のハイライトカラーを任意の色に設定します。また、Silverlightでは、VisualStateManager要素のVisualStateGroup要素内で選択時のハイライトカラーを設定します。
下記は、ハイライトカラーを赤(#FFE94D4D)に設定する例です。

<Window.Resources>
<Style x:Key="SimpleListBoxItem" TargetType="ListBoxItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="1" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<!-- ↓ハイライトカラーを設定-->
<Setter TargetName="Border" Property="Background" Value="#FFE94D4D"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox ItemContainerStyle="{StaticResource SimpleListBoxItem}">
<ListBoxItem>リンゴ</ListBoxItem>
<ListBoxItem>ミカン</ListBoxItem>
<ListBoxItem IsSelected="True">バナナ</ListBoxItem>
<ListBoxItem>パイナップル</ListBoxItem>
<ListBoxItem>スイカ</ListBoxItem>
<ListBoxItem>グレープフルーツ</ListBoxItem>
</ListBox>
</Grid>
<UserControl.Resources>
<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To=".75"
Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="fillColor"/>
<!-- ↓ハイライトカラーを設定-->
<ColorAnimation Duration="0" To="#FFE94D4D"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
Storyboard.TargetName="fillColor" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="fillColor" Fill="#FFBADDE9"
IsHitTestVisible="False" Opacity="0"
RadiusY="1" RadiusX="1"/>
<ContentPresenter x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" Width="616" Content="14.ハイライトカラーを設定する" FontSize="16" />
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
<ListBoxItem Content="リンゴ" />
<ListBoxItem Content="ミカン" />
<ListBoxItem Content="バナナ" />
<ListBoxItem Content="パイナップル" />
<ListBoxItem Content="スイカ" />
<ListBoxItem Content="グレープフルーツ" />
</ListBox>
</Grid>
項目のハイライトカラーを変更するにはListBoxItemのControlTemplateを作成する。WPFの場合は、Trigger要素とSetter要素を使用してハイライトカラーを設定する。
Silverlightの場合はVisualStateManage要素のVisualStateGroup要素を使用してハイライトカラーを設定する。
