我已經爲我的ListBoxItems
定義一個觸發器的風格來設置背景顏色,當IsSelected
爲真:如何一起使用IsKeyboardFocusWithin和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
失去焦點,這在我的情況是絕對必須。 問題是我還希望的TextBox
的一個孩子專注。要做到這一點我想補充一個觸發器,它設定IsSelected
爲真時IsKeyboardFocusWithin
是正確的:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
當我添加此觸發時,重點是對孩子TextBox
的項目被選中,但第一個行爲消失。現在當我點擊ListBox
之外時,該項目被取消選擇。
我該如何保持這兩種行爲?
美麗的僅限XAML解決方案:https://stackoverflow.com/a/15383435/419761 – l33t 2018-02-25 21:57:05