首页 文章

ListBox:未突出显示所选项目

提问于
浏览
3

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

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

问题是当ListBox第一次出现时,所选项目(“One”)不会突出显示 . 如果我点击任何项目,它会突出显示 . 如何将默认选中的项目突出显示为系统颜色?

谢谢 .

3 回答

  • -1

    它被选中但你需要一个没有聚焦的高光

    <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>
    
  • -1
    <Grid FocusManager.FocusedElement="{Binding ElementName=lbUtilities}">
        <ListBox Name="lbUtilities" SelectedIndex="0" >
            <ListBoxItem Content="One"></ListBoxItem>
            <ListBoxItem Content="Two"></ListBoxItem>
        </ListBox>
    </Grid>
    
  • 4

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

    MyListView.SelectedItem = SomeObjectInItemsSource
    

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

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

    然后它开始按预期工作 .

相关问题