2012-04-13 110 views
2

我试图制作一个ListBox,其中突出显示的项目看起来相同,无论ListBox是否具有焦点。设置WPF列表框的非活动突出显示颜色为活动突出显示颜色

本质上我想设置SystemColors.ControlBrushKey颜色属性是与SystemColors.HighlightBrushKey颜色相同。

我想我可以使用以下方法:

<ListBox> 
    <ListBox.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
         Color="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
    </ListBox.Resources> 
</ListBox> 

但其实这引发以下错误:

System.Windows.Markup.XamlParseException:设置属性“System.Windows.Media.SolidColorBrush。颜色'抛出了一个例外。 ---> System.ArgumentException:'#FF3399FF'对属性'Color'不是有效值

如果我设置了Color="#FF3399FF",它工作正常。

我在做什么错?

+3

我想你想的'Color'属性设置为'Brush',不一个'颜色'。 – 2012-04-13 11:21:12

+0

是的,我也这么认为,但现在你确认了我的怀疑,我只注意到我使用了HighlightBrushKey而不是HighlightColorKey * facepalm *我将发布完整的工作示例。谢谢! – 2012-04-13 11:35:17

回答

5

感谢尼古拉W代表指着我在正确的方向 - 在这里是为ListBox的完整代码:

<ListBox Width="200" Height="200"> 
    <ListBox.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
         Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" /> 
    </ListBox.Resources> 
    <ListBox.ItemContainerStyle> 
     <Style> 
      <Style.Triggers> 
       <Trigger Property="Selector.IsSelected" Value="True"> 
        <Setter Property="TextElement.Foreground" Value="White"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBoxItem IsSelected="True">Item A</ListBoxItem> 
    <ListBoxItem>Item B</ListBoxItem> 
    <ListBoxItem>Item C</ListBoxItem> 
</ListBox>