【发布时间】: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>
【问题讨论】: