设置ListBoxItem.IsSelected当孩子的TextBox集中 [英] Set ListBoxItem.IsSelected when child TextBox is Focused

查看:411
本文介绍了设置ListBoxItem.IsSelected当孩子的TextBox集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个典型的MVVM场景:
我有一个绑定到StepsViewModels的List列表框。
我定义一个DataTemplate这样StepViewModels呈现为StepViews。
该StepView用户控件有一组标签和TextBoxs的。

I have a typical MVVM scenario: I have a ListBox that is binded to a List of StepsViewModels. I define a DataTemplate so that StepViewModels are rendered as StepViews. The StepView UserControl have a set of labels and TextBoxs.

我想要做的是选择了包裹StepView当一个TextBox集中在ListBoxItem中。我试图创建一个样式为我TextBoxs具有以下触发器:

What I want to do is to select the ListBoxItem that is wrapping the StepView when a textBox is focused. I've tried to create a style for my TextBoxs with the following trigger:

<Trigger Property="IsFocused" Value="true">
    <Setter TargetName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Property="IsSelected" Value="True"/>
</Trigger>

不过,我得到一个错误,告诉我​​,TextBoxs没有一个IsSelected属性。我现在,但目标是一个ListBoxItem的。
我怎样才能使它工作?

But I get an error telling me that TextBoxs don't have an IsSelected property. I now that but the Target is a ListBoxItem. How can I make it work?

推荐答案

有一个只读属性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>

这篇关于设置ListBoxItem.IsSelected当孩子的TextBox集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆