更改ListBox项边框颜色

时间:2014-02-15 06:57:05

标签: c# wpf windows-phone-8 listbox windows-phone

我有添加了样式的列表框。

这是我的代码:

<!-- Style for list item selector -->
        <Style x:Key="ListItemSelectorStyle" TargetType="ListBoxItem">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property ="Foreground" Value="Black" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="ListBoxItem" 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">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ListItemBorder" BorderBrush="Transparent" Background="#e3e8f0">
                                <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                                    <TextBlock
                                           Name="textBlock"
                                           Text="{Binding Path=answerText}" 
                                           HorizontalAlignment="Stretch"
                                           Padding="10,25,10,25"
                                           MinHeight="80"
                                           VerticalAlignment="Center"
                                           TextAlignment="Center"
                                           Style="{StaticResource TextStyle}"
                                           Foreground="Black"/>
                                    <Image Name="ImageBlock"
                                           Grid.Row="0"
                                           Width="Auto"
                                           Height="Auto"
                                           Stretch="UniformToFill"
                                           Source="{Binding answerImage}"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Margin="1,1,1,1"/>
                                </Grid>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我的列表框:

<ListBox Name="listBox" 
                                 HorizontalAlignment="Stretch"
                                 ItemContainerStyle="{StaticResource ListItemSelectorStyle}"
                                 HorizontalContentAlignment="Stretch" 
                                 VerticalContentAlignment="Stretch" 
                                 SelectionChanged="ListBoxClicked"
                                 ScrollViewer.VerticalScrollBarVisibility="Disabled">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
 </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

这里我为默认项目颜色"#e3e8f0"添加了两种颜色,其中一种是我的样式中的选定项目和MouseOver颜色"#c9ebf2" 现在,我的.CS文件中有一个按钮及其单击事件现在,当我单击时,选中的项目和MouseOver颜色"#c9ebf2"应该更改为绿色,

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

您的样式中鼠标悬停和选定状态都有#c9ebf2颜色。更改所选状态的颜色。