停止高亮显示WPF ComboBox中已选项目

3

我正在开发一个WPF UI,窗口中有一个下拉框(combobox)。

当用户从该下拉框中选择一个项目时,我不希望它以默认蓝色高亮显示。

我认为在XAML中有一些方法可以做到这一点,但我迄今为止还没有找到解决方案。

谢谢。

P.S. 我无法访问Expression Blend,如果有人提出了解决方案,请用XAML实现。

编辑:为了更清楚,我所说的“选择”是指一旦您选择了一个值并且SelectionChanged事件已触发,该项将显示在combobox中,并且combo box会像下面这样高亮显示:

alt text
2个回答

8

您需要通过样式来设置所选择内容的外观。

    <Window.Resources>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border Background="{TemplateBinding Background}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Border Margin="2" Grid.Row="0" Background="Azure" />
                            <ContentPresenter />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

此样式将自动应用于窗口内的任何ComboBox:

<StackPanel>
    <ComboBox>
        <ComboBoxItem>111</ComboBoxItem>
        <ComboBoxItem>222</ComboBoxItem>
        <ComboBoxItem>333</ComboBoxItem>
        <ComboBoxItem>444</ComboBoxItem>
        <ComboBoxItem>555</ComboBoxItem>
    </ComboBox>
</StackPanel>

您将会看到如下内容: http://i.stack.imgur.com/b4pDo.png 更新:为了取消所选项目的高亮显示,您需要修改系统画刷,这些画刷实际上是用于此目的。只需添加两个额外的样式即可。
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>  

谢谢,这很有帮助。但是我的意思是当你选择一个项目(比如333),下拉列表关闭并且所选项目显示在组合框中时,我不想让它像默认情况下那样被突出显示。 - electricsheep
UPD位于'SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"'这一行代码中,解决了我的问题,谢谢。 - user755404

-1
你尝试过直接设置 ComboBox.Background 属性吗?

3
这改变了默认外观的背景,但它并不能阻止当获取焦点时进行突出显示。 - electricsheep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接