【问题标题】:Change the ListBox Item Border Color更改 ListBox 项边框颜色
【发布时间】:2014-02-15 17:46:55
【问题描述】:

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

这是我的代码:

<!-- 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",另一种是我的风格中的选定项目和鼠标悬停颜色"#c9ebf2" 现在我的 .CS 文件中有一个按钮及其单击事件现在,当我单击该选定项和 MouseOver 颜色 "#c9ebf2" 应更改为绿色时,

如何做到这一点?

【问题讨论】:

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


    【解决方案1】:

    您已经为您的样式中的鼠标悬停和选定状态提供了#c9ebf2 颜色。更改所选状态的颜色。

    【讨论】:

    • 不,伙计,这不是问题,当我单击一个按钮时,我需要更改列表框中单行的颜色,该怎么做?
    • 当您选择一个项目或将鼠标悬停在列表框中的一个项目上时,该项目将获得#c9ebf2 颜色。您在列表框之外有一个按钮。单击按钮时,列表框的选定项应更改为另一种颜色,这是您要查找的内容吗?
    • 一种方法是在模板中使用触发器。
    • 怎么样?只有你必须帮助我,我从早上就被困住了
    • 你能根据我的场景举一个例子吗?
    猜你喜欢
    • 2017-03-06
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2013-10-06
    • 2017-03-22
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多