简体   繁体   English

UWP EventTriggerBehaviors按下GotFocus

[英]UWP EventTriggerBehaviors to button GotFocus

I am trying to understand how to setup EventTriggerBehaviors in a UWP project. 我试图了解如何在UWP项目中设置EventTriggerBehaviors。 So I understand that I need to have the package Microsoft.Xaml.Behaviors.Uwp.Managed installed, and the following namespaces declared in my XAML file: 因此,我知道我需要安装软件包Microsoft.Xaml.Behaviors.Uwp.Managed,并在我的XAML文件中声明以下命名空间:

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

The button by itself should be declared as: 该按钮本身应声明为:

<Button x:Name="btnTest >
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="GotFocus" >
            <Core:EventTriggerBehavior.Actions>
                <Core:InvokeCommandAction Command="{Binding ... }" />
            </Core:EventTriggerBehavior.Actions>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Button>

but then I got lost... What I would like is once the button get focus, it set some text (based on button name) within a text box. 但是我迷路了...我想要的是按钮获得焦点后,它会在文本框中设置一些文本(基于按钮名称)。

Do I need a service, and what should be the ViewModel code? 我需要服务吗,ViewModel代码应该是什么?

and actually, is anyone able to recommend great reading, examples, books ... on the subject please? 实际上,有人能推荐关于该主题的精彩阅读,范例,书籍……吗?

Update following James reply: The XAML InvokeCommandAction becomes: 在James答复之后进行更新:XAML InvokeCommandAction变为:

<Core:InvokeCommandAction Command="{Binding OnButtonFocusCommand}" CommandParameter="{Binding Name, ElementName=btnTest}" />

But how do I receive the parameter within the method in the ViewModel? 但是如何在ViewModel的方法中接收参数?

The InvokeCommandAction Command property requires an implementation of an ICommand in your view model in order to perform an action when the EventTriggerBehavior is fired. InvokeCommandAction Command属性要求在您的视图模型中实现ICommand,以便在触发EventTriggerBehavior时执行操作。

You might have something like this in the XAML: 您在XAML中可能会有类似的内容:

    <Button x:Name="btnTest">
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="GotFocus">
                <Core:EventTriggerBehavior.Actions>
                    <Core:InvokeCommandAction Command="{Binding OnButtonFocusCommand}" />
                </Core:EventTriggerBehavior.Actions>
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </Button>

Then in the bound view-model, you would have something similar to this: 然后,在绑定的视图模型中,您将具有以下类似内容:

    public ViewModel()
    {
        OnButtonFocusCommand = new DelegateCommand(() =>
        {
            this.TextBoxText = "Hello, World";
        });
    }

    public ICommand OnButtonFocusCommand { get; private set; }

The DelegateCommand is not built in to the platform though but you can find many implementations of a DelegateCommand or RelayCommand online. 尽管DelegateCommand并未内置在平台中,但是您可以在线找到DelegateCommand或RelayCommand的许多实现。

EDIT: You can also do this with a passed parameter using like this: 编辑:您也可以使用传递的参数来执行此操作,如下所示:

    public ViewModel()
    {
        OnButtonFocusCommand = new DelegateCommand<RoutedEventArgs>(args =>
            {
                this.TextBoxText = "Hello, World";
            });
    }

The RoutedEventArgs would be the type of parameter you're passing through. RoutedEventArgs将是您要传递的参数类型。 In the case of what's passed by the Focus event, this is the parameter that you'll receive. 对于Focus事件传递的内容,这是您将收到的参数。 You'll need the DelegateCommand{T} for these scenarios. 在这些情况下,您将需要DelegateCommand {T}

The examples of DelegateCommand that I've referenced also have a mechanism to check whether to run the action by validating the model. 我参考的DelegateCommand示例还具有一种机制,该机制可通过验证模型来检查是否运行操作。 You can do that like so: 您可以这样做:

    public ViewModel()
    {
        OnButtonFocusCommand = new DelegateCommand<RoutedEventArgs>(args =>
            {
                this.TextBoxText = "Hello, World";
            },
            args => args.OriginalSource is TextBox);
    }

For your scenario with updating the text of a TextBox, you would need to create a property in your view-model (in my example I showed the TextBoxText being updated). 对于更新TextBox文本的方案,您需要在视图模型中创建一个属性(在我的示例中,我显示了TextBoxText正在更新)。 That property would then need binding up to the Text property of your TextBox in XAML. 然后,该属性将需要绑定到XAML中TextBox的Text属性。

For things to take a look at, off the top of my head, I would suggest taking a look at an MVVM framework (possibly MvvmLight) and reading up on that if you've not already. 对于需要看的东西,我建议不要去看MVVM框架(可能是MvvmLight),如果还没有的话,请继续阅读。

Also the official Microsoft samples on GitHub may cover a lot of topics which might be useful to you. 此外,GitHub上的Microsoft官方样本可能涵盖了许多主题,可能对您有用。

If you need any more information, get in touch and I'm happy to help. 如果您需要更多信息,请与我们联系,我们很乐意为您提供帮助。

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

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