且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置ListBoxItem.IsSelected当孩子的TextBox集中

更新时间:2021-12-29 02:59:05

有一个只读属性IsKeyboardFocusWithin如果有孩子的重点是将设置为true。您可以使用此设置ListBoxItem.IsSelected在触发器:

There is a read-only property IsKeyboardFocusWithin that will be set to true if any child is focused. You can use this to set ListBoxItem.IsSelected in a Trigger:

<ListBox ItemsSource="{Binding SomeCollection}" HorizontalAlignment="Left">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsSelected" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Width="100" Margin="5" Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>