Change background color of selected item in listbox

ε祈祈猫儿з 提交于 2019-12-01 11:36:38
McGarnagle

how to change the background color of selected item in listbox

I think you want to change the definition of your ItemContainerStyle. Try something like this:

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ... 

The resource "ListBoxItemStyle1" should contain the control template for ListBoxItem:

<Style TargetType="ListBoxItem" x:Name="ListBoxItemStyle1">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <!-- template here --> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The control template in turn defines the "Selected" visual state. From the page you linked, "ListBoxItemStyle1" defines that visual state as follows (yellow background):

<VisualState x:Name="Selected">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Green"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

Note that, by default, the ListBoxItem's "selected" state uses as its background the user's current "accent brush", as seen below. This is probably the source of the dark violet color that you see. (You can find all default styles and templates in the Windows Phone SDK folder.)

<VisualState x:Name="Selected">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

You can modify this as needed -- copy-paste a default style, either from the Windows SDK, or from the linked page, and set the background and other properties to whatever you want.

For more background info on control templates and visual states, see Customizing the Appearance of an Existing Control by Using a ControlTemplate.

I was just having the same problem. I wanted to get rid of the default blue-purple color, when selecting an item. Even with this post as help it took me a while to figure out which VisualState in the ItemContainerStyle I had to alter. So i thought i'd just post it here. That is what i did:

<VisualStateManager.VisualStateGroups>        
    <VisualStateGroup x:Name="SelectionStates">        
        <VisualState x:Name="SelectedPointerOver">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="[NOT-PURPLE-BLUE-ANYMORE]">
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedForegroundThemeBrush}"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!