更改列表框的整个行颜色

时间:2012-10-24 13:27:50

标签: wpf

所以我一直在努力寻找解决问题的方法。在互联网上,我们可以找到一种可行的方法(link)。

但是在尝试之后,每当我选择列表框中的一个条目时,只有文本变为蓝色(通过Foreground属性),但该行不是红色。你对我做错了什么有任何想法吗?

<Style x:Key="BPFStandardListBoxItem" TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Purple"/>
    </Style.Resources>
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="Blue" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Cursor" Value="Hand" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter> 
</Style>

2 个答案:

答案 0 :(得分:1)

由于您拥有override listboxItem的default template,因此在此过程中您必须拥有broken内容,可能是某些触发器或某些默认属性,因为您的模板已完全覆盖

您可以在不重写此模板的情况下实现您要实现的目标(使用画笔HighlightTextBrushKey设置前景) -

<Style x:Key="BPFStandardListBoxItem" TargetType="{x:Type ListBoxItem}">
   <Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                       Color="Red"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
                       Color="Blue"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                       Color="Purple"/>
   </Style.Resources>
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Cursor" Value="Hand" />
       </Trigger>
    </Style.Triggers>
</Style>

答案 1 :(得分:-1)

嗯,我不确定 - 我以前从未这样做过 - 你总是可以尝试在触发器上设置边框背景

......类似

 <Trigger Property="IsSelected" Value="true">
                <Setter TargetName="Border" Property="Background"
                     Value="Red"/>
   </Trigger>

可在此处找到更多信息http://blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx