2009-04-27 70 views

回答

6

不幸的是,你不能因为它适用时该项目未选择使用SystemColors.ControlTextBrushKey,或当它被选中,但不活跃(你的问题读取,仿佛你是只在后者的兴趣)。但是,你可以这样做:

<ListBox ...> 
    <ListBox.Resources> 
     <!-- this customizes the background color when the item is selected but inactive --> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">Red</SolidColorBrush> 
    </ListBox.Resources> 
    <ListBox.ItemContainerStyle> 
     <Style> 
      <Style.Triggers> 
          <!-- this customizes the foreground color when the item is selected but inactive --> 
       <Trigger Property="Selector.IsSelected" Value="True"> 
        <Setter Property="TextElement.Foreground" Value="Blue"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
+1

谢谢你的回答,但不幸的是所选元素变成灰色,在列表框的时候失去重点:( 我想前景是白色和蓝色的背景,选择元素的时候,但无效。 – 2009-04-27 15:33:52

+0

更新我的答案。 – 2009-04-27 16:16:38

3

对于我这个工作 - 在活动和非活动列表框,选定itemss是相同的前景色和背景色。

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
    <Style.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/> 
    </Style.Resources>   
    </Style> 
</ListBox.ItemContainerStyle>