SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Silverlight/WPFで使える逆引きTips集

Silverlight/WPFで使える逆引きTips集
――リストボックス機能

(4) ListBoxコントロール


ダウンロード WPF_VB.zip (322.6 KB)
ダウンロード WPF_CS.zip (212.4 KB)
ダウンロード Silverlight_VB.zip (2.4 MB)
ダウンロード Silverlight_CS.zip (2.3 MB)

13. 項目を削除する

 単一の項目を削除するにはItemsプロパティRemoveAtメソッドを使用します。RemoveAtメソッドの引数には削除したい項目のインデックス番号を指定します。また、すべての項目を削除したい場合にはItemsプロパティのClearメソッドを使用します。

 下記は、「選択項目の削除」および「全項目の削除」をする例です。

項目を削除する例
項目を削除する例
VBの例
'[選択項目を削除]ボタンクリック時の処理
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
C#の例
// [選択項目を削除]ボタンクリック時の処理
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)に設定する例です。

ハイライトカラーを設定する例
ハイライトカラーを設定する例
WPFのXAMLの例
<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>

SilverlightのXAMLの例
<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要素を使用してハイライトカラーを設定する。

次のページ
15. 選択項目が変更されたことを知る

この記事は参考になりましたか?

Silverlight/WPFで使える逆引きTips集連載記事一覧

もっと読む

この記事の著者

HIRO(ヒロ)

HIRO's.NETのHIROです。とある半導体工場のSEです。VB.NET, C#, PowerShellによるプログラミングを楽しんでいます。最近はBlog でPowerShellについて書いています。2008/07/07にPowerShell from Japan!!というサイトを立ち上げまし...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/5557 2010/11/25 22:09

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー