• 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.

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

  • 相关阅读:
    python学习之__doc__,__module__,__class__,__del__,__call__,__iter__,__next__
    20180910
    20100911
    20180912
    服装分销
    道讯商品条码管理
    [新版新概念英语14册全部视频和课本]
    2012/06/18
    《鬼谷子本经阴符七术》
    归来
  • 原文地址:https://www.cnblogs.com/626498301/p/2280482.html
Copyright © 2020-2023  润新知