停止高亮所选项目的WPF组合框 [英] Stop highlighting selected item WPF ComboBox
问题描述
我目前工作的一个WPF UI和我有我的窗口中的组合框。 所以,我希望用户能够选择该组合框的项目,但在选择时, 我不希望它在默认的蓝色高亮显示。
I'm currently working on a WPF UI and I have a combobox on my window. So I want the user to be able to select an item from this combobox but when it is selected I don't want it to be highlighted in the default blue colour.
我想有一些办法阻止这在XAML,但我一直没能找到解决办法为止。
I assume there is some way to stop this in XAML but I have not been able to find the solution so far.
感谢。
P.S。我没有进入前pression混合所以如果有人提出了一个解决方案,它可以在XAML
P.S. I don't have access to Expression Blend so if anyone suggests a solution could it be in XAML
编辑:只是为了更清楚地我被选中我的意思是,一旦你选择了一个值,SelectionChanged事件已触发,而该项目显示在组合框和组合框,像这样强调:
Just to make it clearer I by selected I mean once you have selected a value and the SelectionChanged event has fired and the item is displayed in the combobox and the combo box is highlighted like so:
推荐答案
您需要通过样式设置外观选择的。
You need to set appearance of your selection via styles.
<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>
这个样式将被自动应用到窗口内的任何组合框:
This style will be automatically applied to any ComboBoxes within the window:
<StackPanel>
<ComboBox>
<ComboBoxItem>111</ComboBoxItem>
<ComboBoxItem>222</ComboBoxItem>
<ComboBoxItem>333</ComboBoxItem>
<ComboBoxItem>444</ComboBoxItem>
<ComboBoxItem>555</ComboBoxItem>
</ComboBox>
</StackPanel>
您将看到如下:
UPD:为了去除选定的项目突出了需要修改系统刷其实际用于这些目的。只需添加两个额外的风格:
UPD: In order to remove highlighting from selected item you need to modify system brushes which are actually used for these purposes. Just add two extra styles:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
这篇关于停止高亮所选项目的WPF组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!