我有一个为我定义的样式,当它为 TrueListBoxItems
时,触发器可以设置背景颜色:IsSelected
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
即使在ListBox
和ListBoxItem
失去焦点时,这种风格也会保持选定的项目,这在我的情况下是绝对必须的。问题是我还希望在ListBoxItem
它TextBox
的一个孩子集中注意力时选择 。为了实现这一点,我添加了一个触发器,当为IsSelected
真时设置IsKeyboardFocusWithin
为真:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
当我添加此触发器时,当焦点位于 child 时选择了 Item TextBox
,但第一个行为消失了。现在,当我在 外部单击时ListBox
,该项目被取消选择。
我怎样才能保持这两种行为?