0

所以我已经尝试了几个小时来解决我的问题。在互联网上,我们可以找到一种据说有效的方法(链接)。

但是在尝试之后,每当我选择列表框中的一个条目时,只有文本变为蓝色(通过 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>
4

2 回答 2

1

由于您拥有overridelistboxItem 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>
于 2012-10-24T14:44:35.333 回答
-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

于 2012-10-24T13:46:36.153 回答