Q100. リストボックスの選択行の背景色を変更するには?

A.SystemColors.HighlightBrushKey を上書きします。以下のサンプルでは選択行の背景色を赤に変更しています。また SystemColors.ControlBrushKey を上書きすると、リストボックスのフォーカスが外れても選択行の強調表示を保持することが出来ます。


<ListBox ItemsSource="{Binding Persons}"
          IsSynchronizedWithCurrentItem="True" SelectedIndex="0" Grid.Row="1" >
    <ListBox.Resources>
        <!-- 選択行・フォーカス時の背景色 -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
        <!-- 選択行・非フォーカス時の背景色 -->
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name, Mode=OneWay}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


WPF FAQ の目次に戻る