wpf 列表框:选定的项目未突出显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17725488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:14:10  来源:igfitidea点击:

ListBox: Item selected is not highlighted

wpflistbox

提问by David Shochet

In my WPF application, I have a simple listbox:

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

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

问题是当 ListBox 第一次出现时,所选项目(“一个”)没有突出显示。如果我单击任何项​​目,它就会突出显示。如何让默认选择的项目突出显示为系统颜色?

Thanks.

谢谢。

回答by paparazzo

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>

回答by Nitesh

<Grid FocusManager.FocusedElement="{Binding ElementName=lbUtilities}">
    <ListBox Name="lbUtilities" SelectedIndex="0" >
        <ListBoxItem Content="One"></ListBoxItem>
        <ListBoxItem Content="Two"></ListBoxItem>
    </ListBox>
</Grid>

回答by Michel P.

I had a similar problem with the listview control where the selected item was highlighted only from user clicking on it, not from code behind such as:

我在 listview 控件中遇到了类似的问题,其中所选项目仅在用户单击时突出显示,而不是从后面的代码中突出显示,例如:

MyListView.SelectedItem = SomeObjectInItemsSource

I saw that the item was effectively selected but not highlighted as defined in my ItemContainerStyle. Then i tried something else:

我看到该项目已被有效选择,但未按照我的 ItemContainerStyle 中的定义突出显示。然后我尝试了别的东西:

With CType(MyListView.ItemsSource,IList) 
    .MyListView.SelectedIndex = .IndexOf(SomeObjectInItemsSource)
End With

then it started to work just as expected.

然后它开始按预期工作。