更改列表框中所选项目的背景颜色 [英] Change background color of selected item in listbox

查看:124
本文介绍了更改列表框中所选项目的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我在这里和网上搜索,发现了很多解决方案,如何在WPF中更改列表框中所选项目的背景颜色,而不是如何在Windows Store应用程序中更改背景颜色.这个框架有点不同,我无法使用任何解决方案.

First I search here and on web and I find many and many solution how change background color of selected item in listbox in WPF but not how do it in windows store app. This framework is litte different I cant get to work any solution.

我使用以下代码: 但是我的列表框有datateplate,所以我不知道如何通过setter或任何其他方式设置ItemTemplate样式.

I use this: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/91575930-2058-413a-99de-f3b31c74dfd9/change-itemtemplate-forground-when-listbox-is-focused?forum=winappswithcsharp on the end of page is very good solution but he set item teplate like this: ItemTemplate="{StaticResource DataTemplate1}" but my listbox has datateplate so I dont know how set ItemTemplate style via setter or any different way.

我的列表框:

<ListBox x:Name="lbMenu" ItemsSource="{Binding MyDataForLunchGrid}" Tapped="lbMenzaMenu_Tapped" Style="{StaticResource ListBoxStyle1}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Style" Value="{StaticResource ListBoxItemStyle1}"/>
        </Style>
    </ListBox.ItemContainerStyle >
    <ListBox.ItemTemplate >
        <DataTemplate>
            <Grid>
                <TextBlock Foreground="#FF19536E"  x:Name="tbMenu" Text="{Binding launchItemName}"/>
                <TextBlock x:Name="tbMenuNumber" Text="{Binding launchNumber}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

现在,当我按列表框中的任何项目时,它的颜色是深紫色(默认),看起来很可怕.

Now when I press any item in listbox its have dark violet(default) color and its look horible.

推荐答案

如何更改列表框中所选项目的背景颜色

how to change the background color of selected item in listbox

我认为您想更改ItemContainerStyle的定义.尝试这样的事情:

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

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

资源"ListBoxItemStyle1"应包含ListBoxItem的控制模板:

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>

请注意,默认情况下,ListBoxItem的选定"状态使用用户当前的重音刷"作为背景,如下所示.这可能是您看到的深紫色的来源. (您可以在 Windows Phone SDK文件夹中找到所有默认样式和模板.)

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>

您可以根据需要进行修改-从Windows SDK或链接页面复制并粘贴默认样式,然后将背景和其他属性设置为所需的任何样式.

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.

有关控件模板和视觉状态的更多背景信息,请参见使用ControlTemplate自定义现有控件的外观.

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

这篇关于更改列表框中所选项目的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆