列表框:所选项目未突出显示 [英] ListBox: Item selected is not highlighted

查看:92
本文介绍了列表框:所选项目未突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF应用程序中,我有一个简单的列表框:

In my WPF application, I have a simple listbox:

                 <ListBox x:Name="lbUtilities">
                    <ListBoxItem Tag="2" Content="One" IsSelected="True" />
                    <ListBoxItem Tag="5" Content="Two" />
                 </ListBox>

问题在于,当列表框第一次出现时,所选项目(一个")没有突出显示.如果我单击任何项​​目,它将突出显示.如何默认选择要突出显示为系统颜色的项目?

The problem is that when the ListBox appears first time, the selected item ("One") is not highlighted. If I click on any item, it gets highlighted. How could I have the selected by default item to be highlighted to the system color?

谢谢.

推荐答案

它已被选中,但您需要突出重点才能解决

It is selected but you need a hightlight for not focused

<ListBox Grid.Row="0" x:Name="lbUtilities">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True" >
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Foreground" Value="Black" />
                </Trigger>
            </Style.Triggers>
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                <!-- Background of selected item when focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightCyan"/>
                <!-- Background of selected item when not focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGray" />
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem Tag="2" Content="One" IsSelected="True"/>
    <ListBoxItem Tag="5" Content="Two" />
</ListBox>

这篇关于列表框:所选项目未突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆