停止突出显示所选项目WPF ComboBox

编程入门 行业动态 更新时间:2024-10-25 12:29:25
本文介绍了停止突出显示所选项目WPF ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在开发一个WPF UI,我的窗口上有一个组合框。 所以我想让用户能够从这个组合框中选择一个项目,但是当它被选中我不想让它以默认的蓝色突出显示。

我假设在XAML中有一些方法可以阻止这个问题,但是到目前为止我还没有找到解决方案。

谢谢。 / p>

PS我没有访问Expression Blend,所以如果有人建议一个解决方案可以在XAML

编辑:只是为了使它更清晰我选择我的意思是一旦你有选择了一个值,并且SelectionChanged事件已触发,并且项目显示在组合框中,并且组合框突出显示为:

解决方案

您需要通过样式设置选择的外观。 $ b

< 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 =2Grid.Row =0Background =Azure/> < ContentPresenter /> < / Grid> < / Border> < ControlTemplate.Triggers> < Trigger Property =IsMouseOverValue =True> < Setter Property =BackgroundValue =Green/> < / Trigger> < /ControlTemplate.Triggers> < / ControlTemplate> < /Setter.Value> < / Setter> < / Style> < /Window.Resources>

此样式将自动应用于窗口中的任何ComboBoxes:

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

您将看到如下:

UPD:为了从选定的项目中删除突出显示,您需要修改实际用于这些用途的系统画笔。只需添加两个额外的样式:

< SolidColorBrush x:Key ={x:Static SystemColors.HighlightBrushKey}透明/> < SolidColorBrush x:Key ={x:Static SystemColors.HighlightTextBrushKey}Color =Black/>

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.

I assume there is some way to stop this in XAML but I have not been able to find the solution so far.

Thanks.

P.S. I don't have access to Expression Blend so if anyone suggests a solution could it be in XAML

EDIT: 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>

You will see it as follows:

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 ComboBox

本文发布于:2023-06-10 02:04:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/606272.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所选   项目   ComboBox   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!