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

发布于 2024-09-26 00:19:48 字数 334 浏览 1 评论 0原文

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

我认为 XAML 中有某种方法可以阻止此问题,但到目前为止我还没有找到解决方案。

谢谢。

PS我无权访问Expression Blend,所以如果有人提出解决方案,可以在XAML中

编辑:只是为了更清楚,我选择的意思是一旦您选择了一个值并且SelectionChanged事件已触发并且该项目被显示在组合框中,组合框突出显示如下: 替代文本

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:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま昔日黯然 2024-10-03 00:19:48

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

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

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

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

您将看到如下所示:

https://i.sstatic. net/b4pDo.png

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

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

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:

https://i.sstatic.net/b4pDo.png

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"/>  
ゝ杯具 2024-10-03 00:19:48

您是否尝试仅设置 ComboBox.Background 属性?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文