zoukankan      html  css  js  c++  java
  • Behavior:FocusOnEvent

         最近一段时间,被各种事情困扰,博客一直没有更新,今天分享一个简单的技巧.

         加载页面时,需要Focus某个TextBox,在CodeBehind下这很容易实现,如果采用MVVM模式时,这是与ViewModel无关的,但是可以用Behavior实现.

         这里需要用到TargetedTriggerAction<T>,这个T需要是一个Control,因为这样才可以Focus:

       public class FocusOnEvent : TargetedTriggerAction<Control>  
       {
           protected override void Invoke(object o)
           {
               HtmlPage.Plugin.Focus();
               Target.Focus();
           }
       }

        我们可以给它添加一个DefaultTrigger,在Loaded事件中进行触发

    [DefaultTrigger(typeof(FrameworkElement), typeof(EventTrigger), "Loaded")] 

      现在Behavior定义好了,在UI上引用该Behavior

    <i:Interaction.Triggers>
        <i:EventTrigger>
            <local:FocusOnEvent TargetName="textBox1"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

       这样当页面加载成功后,textBox1就会被设为默认焦点的Control.

       这篇就到这里,希望对你有所帮助.

  • 相关阅读:
    probuf了解
    rebbitmq-RPC(C#)
    获取指定数量的有序列表
    RPC(Remote Procedure Call Protocol)
    正则表达式(c#)
    02_python函数
    01_python基础
    养生茶
    测试左右移浅淡
    python类的组合
  • 原文地址:https://www.cnblogs.com/626498301/p/2280482.html
Copyright © 2011-2022 走看看