列表框:所选项目未突出显示

2024-04-15

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

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

问题是,当列表框第一次出现时,所选项目(“One”)不会突出显示。如果我点击任何项目,它就会突出显示。如何让默认选择的项目突出显示为系统颜色?

Thanks.


它已被选中,但您需要突出显示不聚焦

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

列表框:所选项目未突出显示 的相关文章

随机推荐