ListBox isSelected方法更改文本样式

时间:2014-03-30 21:58:40

标签: windows-phone-8

再次简单的菜鸟问题。我有一个Windows 8手机应用程序,其中ListBox加载了ListBoxItems(只是纯文本)。我在xaml中将文本的前景色设置为白色:

<ListBox x:Name="L1" Foreground="white">

一旦我调用ListBox的SelectedIndex属性,前景就会变为红色。无论我是在xaml还是c#中设置它都会这样做。如果我尝试在c#中添加代码以在调用SelectedIndex后手动更改颜色,它仍然无法正常工作......

tempListBoxItem = listBoxPicType.SelectedItem as ListBoxItem;
tempListBoxItem.Foreground = //some color that isn't red

解决这个问题的最简单方法是什么? TIA

1 个答案:

答案 0 :(得分:1)

它有一个简单的方法来实现它,你可以改变ListBoxItem ControlTemplate。这是详细信息的代码

您可以将style放在PhoneApplicationPage的资源中,更改Selected VisualState

<Style x:Key="ListBoxItemStyle1" 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" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <!--The selected state, change the value to your color-->
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Your Color"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ListBox

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}"/>

希望这可以帮到你。感谢