开发者

Stop highlighting selected item WPF ComboBox

开发者 https://www.devze.com 2023-01-18 09:11 出处:网络
I\'m currently working on a WPF UI and I have a combobox on my window. So I want the user to be开发者_StackOverflow able to select an item from this combobox but when it is selected

I'm currently working on a WPF UI and I have a combobox on my window. So I want the user to be开发者_StackOverflow 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:

Stop highlighting selected item WPF ComboBox


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:

Stop highlighting selected item WPF ComboBox

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"/>  


Did you try to just set the ComboBox.Background property ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号