简体   繁体   English

如何在ListView项的TextBox中设置焦点?

[英]How to set the focus in a TextBox of a ListView item?

On a UWP app (Windows 10), I am displaying a list of records in a ListView. 在UWP应用(Windows 10)上,我在ListView中显示记录列表。

When I click on an item, its StackPanel is displayed (using INotifyPropertyChanged). 当我单击一个项目时,将显示其StackPanel(使用INotifyPropertyChanged)。 In the StackPanel, there is a TextBox with some data populated via binding. 在StackPanel中,有一个TextBox,其中一些数据是通过绑定填充的。

I would like that the TextBox automatically receives the focus whenever the StackPanel becomes visible, but I can't find which property or event to use, and how to trigger a textBox.Focus(). 我希望每当StackPanel变得可见时,TextBox都会自动获得焦点,但是我找不到要使用的属性或事件,以及如何触发textBox.Focus()。

Thanks for your feedback on this ! 感谢您对此的反馈!

The DataTemplate: 数据模板:

    <DataTemplate x:Key="templateList">
        <StackPanel>
...
            <StackPanel Visibility="{Binding IsSelected}">
                <TextBox x:Name="textBox"
                         Text="{Binding Title, Mode=TwoWay}"/>
...
            </StackPanel>
        </StackPanel>
    </DataTemplate>
...

The ListView: ListView:

<ListView x:Name="listView" 
          ItemsSource="{Binding mylist}" 
          ItemTemplate="{StaticResource templateList}"/>

I can suggest use Behaviors for this case. 我可以建议在这种情况下使用Behaviors As I noticed, you use Visibility type for IsSelected property. 正如我所注意到的,您对IsSelected属性使用Visibility类型。 It means that we can use DataTriggerBehavior and create our SetupFocusAction which implement IAction 这意味着我们可以使用DataTriggerBehavior并创建实现IAction SetupFocusAction

public class SetupFocusAction : DependencyObject, IAction
{
    public Control TargetObject
    {
        get { return (Control)GetValue(TargetObjectProperty); }
        set { SetValue(TargetObjectProperty, value); }
    }

    public static readonly DependencyProperty TargetObjectProperty =
        DependencyProperty.Register("TargetObject", typeof(Control), typeof(SetupFocusAction), new PropertyMetadata(0));

    public object Execute(object sender, object parameter)
    {
        return TargetObject?.Focus(FocusState.Programmatic);
    }
}

After that we can use this action in XAML: 之后,我们可以在XAML中使用此操作:

xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

...

<StackPanel Visibility="{Binding IsSelected}"
            Grid.Row="1">
    <TextBox x:Name="textBox"
                Text="{Binding Title, Mode=TwoWay}">
        <i:Interaction.Behaviors>
            <core:DataTriggerBehavior Binding="{Binding IsSelected}"
                                        ComparisonCondition="Equal"
                                        Value="Visible">
                <local:SetupFocusAction TargetObject="{Binding ElementName=textBox}"/>
            </core:DataTriggerBehavior>
        </i:Interaction.Behaviors>
    </TextBox>
</StackPanel>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM