如何在Windows Phone 8.1中指定所选ListBoxItem的样式?

时间:2014-07-16 14:58:50

标签: xaml windows-phone windows-phone-8.1

目前,当我在列表中选择一个项目时,它会将项目设置为蓝色背景。我正在尝试指定列表项应该没有背景,即在选中时是透明的。

到目前为止,我将ListBox定义为:

<ListBox Name="LogsListBox" Grid.Row="1" Background="Transparent">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Background" Value="Transparent" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

但是当我选择列表项时它仍然显示为蓝色。

选择后,如何使列表项透明,我需要做什么?

2 个答案:

答案 0 :(得分:0)

您应该在项目的某处定义一个名为“ListViewItemSelectedBackgroundThemeBrush”的画笔,如下所示:

<SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#FF4617B4" />

如果没有,请在您的应用程序资源字典中定义一个新的。

答案 1 :(得分:0)

我找到了这个答案并且有效:

https://stackoverflow.com/a/7359024/181771

基本上我把它粘贴到我的UserControl中:

<UserControl.Resources>
    <Style TargetType="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" 
                            Background="{TemplateBinding Background}" 
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                            VerticalAlignment="{TemplateBinding VerticalAlignment}" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames 
                                            Storyboard.TargetName="LayoutRoot" 
                                            Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" 
                                                                    Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation 
                                            Storyboard.TargetName="ContentContainer" 
                                            Storyboard.TargetProperty="Opacity" Duration="0" To=".5" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames 
                                            Storyboard.TargetName="ContentContainer" 
                                            Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentControl x:Name="ContentContainer" 
                                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
                                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"  
                                        Margin="{TemplateBinding Padding}" 
                                        Content="{TemplateBinding Content}" 
                                        ContentTemplate="{TemplateBinding ContentTemplate}" 
                                        Foreground="{TemplateBinding Foreground}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>