簡體   English   中英

當子控件具有焦點時如何在WPF列表框上鎖定SelectedIndex

[英]How to latch the SelectedIndex on a WPF listbox when a child control has focus

我目前有

<Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="IsSelected" Value="True" />
        </Trigger>
    </Style.Triggers>
</Style>

但是問題是當我對UI焦點進行其他控制時,SelectedIndex返回-1 我正在尋找的行為是當孩子獲得焦點時選擇了ListBoxItem但當孩子失去焦點時不會重置。

EventTrigger替換Trigger ,然后IsSelected不會返回到原始狀態:

<EventTrigger RoutedEvent="PreviewGotKeyboardFocus">
    <BeginStoryboard>
        <Storyboard>
            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0" />
            </BooleanAnimationUsingKeyFrames>
        </Storyboard>
    </BeginStoryboard>                          
</EventTrigger>

MSDN

與Trigger不同,EventTrigger沒有終止狀態的概念,因此一旦引發事件的條件不再成立,該動作將不會撤消。

PS:我只為SelectionMode="Single"嘗試過此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM