「Androidは電気羊の夢を見るか」を読みたい管理者のブログ

仕事などでの色々な発見を記事にしてます。不定期更新。

ListBoxで選択されてるアイテムの背景色を青から違う色にしたい

どうしてこんな簡単なことにこんな苦労しなきゃいけないんだろうね
それはお前に実力がないからだぷぎゃー
ごめんなさいごめんなさい

というわけで。

出来てみたら簡単だった話

<ListBox>
            <ListBox.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border x:Name="Bd"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}"
                                        Background="{TemplateBinding Background}"
                                        Padding="{TemplateBinding Padding}"
                                        SnapsToDevicePixels="true">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="Selector.IsSelectionActive"
                                                        Value="False" />
                                            <Condition Property="IsSelected"
                                                        Value="True" />
                                        </MultiTrigger.Conditions>
                                        <Setter Property="Background"
                                                TargetName="Bd"
                                                Value="DarkOrange" />
                                    </MultiTrigger>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="Selector.IsSelectionActive"
                                                        Value="True" />
                                            <Condition Property="IsSelected"
                                                        Value="True" />
                                        </MultiTrigger.Conditions>
                                        <Setter Property="Background"
                                                TargetName="Bd"
                                                Value="OrangeRed" />
                                    </MultiTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.Resources>

blog.jsinh.in

ポイント①リストボックス内でDataTemplateを定義してItemをBorderで囲みます。
ポイント②Borderに「Bd」って名前をつけます。
ポイント③Triggerを定義して選択された時に②で名前をつけたBorderをターゲットとして背景色を変えます。
以上。

出来てみたら簡単だった。