首页 文章

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

提问于
浏览
2

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

我用这个:页面末尾的http://social.msdn.microsoft.com/Forums/windowsapps/en-US/91575930-2058-413a-99de-f3b31c74dfd9/change-itemtemplate-forground-when-listbox-is-focused?forum=winappswithcsharp是非常好的解决方案,但他设置了这样的项目teplate: ItemTemplate="{StaticResource DataTemplate1}" 但我的列表框有数据板,所以我不知道如何通过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 的定义 . 尝试这样的事情:

    <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"可视状态 . 从page you linked开始,"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 's 2374237 state uses as its background the user'的当前"accent brush",如下所示 . 这可能是你看到的暗紫色的来源 . (您可以在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或链接页面复制粘贴默认样式,并将背景和其他属性设置为您想要的任何属性 .

    有关控件模板和可视状态的更多背景信息,请参阅Customizing the Appearance of an Existing Control by Using a 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>
    

相关问题