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

3

我在这里和网络上搜索了很多关于如何更改WPF Listbox中所选项的背景颜色的解决方案,但没有找到关于如何在Windows Store应用程序中实现的方法。该框架与其他框架略有不同,我无法使任何解决方案正常工作。

我使用了这个:http://social.msdn.microsoft.com/Forums/windowsapps/en-US/91575930-2058-413a-99de-f3b31c74dfd9/change-itemtemplate-forground-when-listbox-is-focused?forum=winappswithcsharp 本页末尾提供了一个非常好的解决方案,但他将项目模板设置为: ItemTemplate="{StaticResource DataTemplate1}" 而我的ListBox具有DataTemplate,因此我不知道如何通过setter或任何其他方式设置ItemTemplate样式。

我的列表框:

<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>

现在,当我按下列表框中的任何项时,它们都呈现出深紫色(默认)颜色,看起来很糟糕。

2个回答

3
如何更改列表框中所选项目的背景颜色
我认为您想要更改ItemContainerStyle的定义。尝试像这样做:
```html ```
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ... 

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

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

控件模板进一步定义了“Selected”视觉状态。从您链接的页面中,“ListBoxItemStyle1”如下定义该视觉状态(黄色背景):
<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的“selected”状态使用用户当前的“强调刷子”作为其背景,如下所示。这可能是您看到的深紫色的来源。(您可以在Windows Phone SDK文件夹中找到所有默认样式和模板。)
<VisualState x:Name="Selected">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

你可以根据需要进行修改 -- 从Windows SDK或链接页面复制默认样式,并将背景和其他属性设置为所需内容。
有关控件模板和可视状态的更多背景信息,请参见 使用ControlTemplate自定义现有控件的外观

1

我刚刚遇到了同样的问题。我想要更改默认的蓝紫色选中项颜色。即使有这篇帖子作为帮助,我也花了一些时间才弄清楚我需要修改ItemContainerStyle中哪个VisualState。所以我想在这里发布一下我的方法:

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接