【问题标题】:How to keep highlighting selected item of listbox even listbox lost focus?即使列表框失去焦点,如何保持突出显示列表框的选定项目?
【发布时间】:2019-08-19 09:08:09
【问题描述】:

我有一个 ListBox,其中 ItemsSource 绑定到 ObservableCollection

Item 有两个字符串属性 Name 和 Description。

Item 的 DataTemplate 是具有子项的 StackPanel:TextBlock 与 Text 绑定到 Name,TextBox 与 Text 绑定到 Description。

我现在拥有的: 1.当光标在一个item的Description对应的TextBox时,对应的ListBoxItem被高亮显示。 2. 我可以使用 Tab 在项目的文本框之间导航 3. 如果我将光标移动到列表框之外的另一个名为 TextBox(下面代码中的 TextBox),则所选项目不再突出显示。那是我的问题。

https://drive.google.com/file/d/1tyxaBLnnjFUCJRTsHbwBwSvdU_X_L1fn/view?usp=sharing 的 png 帮助解释了我的问题。

<DockPanel>
        <ListBox ItemsSource="{Binding Items}" DockPanel.Dock="Top" Height="100" KeyboardNavigation.TabNavigation="Cycle">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="IsTabStop" Value="False"/>
                    <Setter Property="Focusable" Value="False"/>
                    <Style.Triggers>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="Background" Value="LightGreen" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <ItemContainerTemplate >
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Name}"/>
                        <TextBox Text="{Binding Description}"/>
                    </StackPanel>
                </ItemContainerTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBox Name-"theTextBox" AcceptsReturn="True" />
    </DockPanel>

【问题讨论】:

    标签: wpf listbox


    【解决方案1】:

    未对焦时的背景不同,因为它使用了不同的画笔。

    如果这是您想将其应用于所有内容并且您不在乎它们是否集中,那么您可以覆盖系统颜色。 在 app.xaml 合并的资源字典中添加:

    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
     Color={x:Static SystemColors.HighlightColor}"/>
    

    【讨论】:

    • ``` 如果我添加 正如你在 标签中所说的那样。我收到错误消息“缺少空格字符”```
    • "在由 app.xaml 合并的资源字典中" 您是否将键入的内容放入资源字典中?您是否在 app.xaml 中合并了该资源字典?似乎两者都没有。此处的答案显示 app.xaml stackoverflow.com/questions/27215155/… 中资源字典的合并
    • 它不起作用。我的代码在github.com/hjy1210/TestListBox。当我按 Tab 时,光标在 Item 的 TextBox 之间导航,突出显示相应的 ListBoxItem。当我将注意力集中在列表框之外时,不会在任何 LIstBoxItem 上突出显示。
    【解决方案2】:

    这是我为 ListBoxItems 全局使用的样式,包括即使在控件失去焦点时也保持所选项目突出显示。

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <!--  Revert to the "Windows 7" style template that used "SystemColors.HighlightBrushKey" etc  -->
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border
                        x:Name="ItemBorder"
                        Padding="{TemplateBinding Padding}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
    
                    <ControlTemplate.Triggers>
                        <!--  Use the same colours for selected items, whether or not the control has focus  -->
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="ItemBorder" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                        </Trigger>
    
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 假设 ListBox 的 itemtemplate 包含 TextBlock 和 TextBox 作为我的 senario。然后我们需要按两次 Tab 键导航到下一个 ListBoxItem 的 TextBox。
    • 假设 ListBox 的 itemtemplate 包含 TextBlock 和 TextBox 作为我的 senario。然后我们需要按两次 Tab 键导航到下一个 ListBoxItem 的 TextBox。但我希望只有一个 Tab 来完成这项工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2011-03-29
    • 2013-12-22
    相关资源
    最近更新 更多