2009-04-27 67 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>