我目前正在开发一个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
发布评论