3

我正在尝试制作一个 ListBox,无论 ListBox 是否具有焦点,突出显示的项目看起来都相同。

本质上,我想将 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.Color”引发异常。---> System.ArgumentException: '#FF3399FF' 不是属性 'Color' 的有效值

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

我究竟做错了什么?

4

1 回答 1

5

感谢 Nicholas 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>
于 2012-04-13T11:36:12.023 回答