原文:整理:WPF用于绑定命令和触发路由事件的自定义控件写法

目的:自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分)

自定义控件增加ICommand


  1. #region - 用于绑定ViewModel部分 -
  2. public ICommand Command
  3. {
  4. get { return (ICommand)GetValue(CommandProperty); }
  5. set { SetValue(CommandProperty, value); }
  6. }
  7. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  8. public static readonly DependencyProperty CommandProperty =
  9. DependencyProperty.Register("Command", typeof(ICommand), typeof(MyUserControl), new PropertyMetadata(default(ICommand)));
  10. public object CommandParameter
  11. {
  12. get { return (object)GetValue(CommandParameterProperty); }
  13. set { SetValue(CommandParameterProperty, value); }
  14. }
  15. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  16. public static readonly DependencyProperty CommandParameterProperty =
  17. DependencyProperty.Register("CommandParameter", typeof(object), typeof(MyUserControl), new PropertyMetadata(default(object)));
  18. public IInputElement CommandTarget { get; set; }
  19. #endregion

自定义增加路由事件


  1. #region 用于Xaml触发路由事件部分
  2. //声明和注册路由事件
  3. public static readonly RoutedEvent MyEventRoutedEvent =
  4. EventManager.RegisterRoutedEvent("MyEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(MyUserControl));
  5. //CLR事件包装
  6. public event RoutedEventHandler MyEvent
  7. {
  8. add { this.AddHandler(MyEventRoutedEvent, value); }
  9. remove { this.RemoveHandler(MyEventRoutedEvent, value); }
  10. }
  11. //激发路由事件,借用Click事件的激发方法
  12. protected void OnMyEvent()
  13. {
  14. RoutedEventArgs args = new RoutedEventArgs(MyEventRoutedEvent, this);
  15. this.RaiseEvent(args);
  16. }
  17. #endregion

自定义控件中增加一个按钮,当按钮点击时触发绑定的命令和路由事件


  1. /// <summary> 内部触发的路由事件和自定义命令方法 </summary>
  2. private void Button_Click(object sender, RoutedEventArgs e)
  3. {
  4. //命令作用于命令目标
  5. if (this.Command != null)
  6. {
  7. this.Command.Execute(CommandParameter);
  8. this.OnMyEvent();
  9. }
  10. }

Xaml部分绑定ICommand和触发RoutedEvent


  1. <local:MyUserControl Grid.Row="1" Command="{Binding RelayCommand}" CommandParameter="Sumit">
  2. <local:MyUserControl.Triggers>
  3. <EventTrigger RoutedEvent="local:MyUserControl.MyEvent">
  4. <BeginStoryboard>
  5. <Storyboard>
  6. <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" BeginTime="00:00:00" Duration="00:00:01"/>
  7. </Storyboard>
  8. </BeginStoryboard>
  9. </EventTrigger>
  10. </local:MyUserControl.Triggers>
  11. </local:MyUserControl>

Command用于绑定ViewModel中的Command

EventTrigger用于注册路由事件

效果:当点击内部按钮是触发了ViewModel的同时也触发了动画效果

整理:WPF用于绑定命令和触发路由事件的自定义控件写法的更多相关文章

  1. ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法

    转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...

  2. WPF手动触发路由事件

    MouseButtonEventArgs args = , MouseButton.Left); args.RoutedEvent = UIElement.MouseLeftButtonDownEve ...

  3. WPF 自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分)

    #region - 用于绑定ViewModel部分 - public ICommand Command { get { return (ICommand)GetValue(CommandPropert ...

  4. 迟到的 WPF 学习 —— 路由事件

    1. 理解路由事件:WPF 通过事件路由(event routing)概念增强了传统的事件执行的能力和范围,允许源自某个元素的事件由另一个元素引发,例如,事件路由允许工具栏上的一个按钮点击的事件在被代 ...

  5. 细说WPF自定义路由事件

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件   W ...

  6. WPF自学入门(三)WPF路由事件之内置路由事件

    有没有想过在.NET中已经有了事件机制,为什么在WPF中不直接使用.NET事件要加入路由事件来取代事件呢?最直观的原因就是典型的WPF应用程序使用很多元素关联和组合起来,是否还记得在WPF自学入门(一 ...

  7. WPF Demo18 路由事件

    using System.Windows; namespace 路由事件2 { public class Student { ////声明并定义路由事件 //public static readonl ...

  8. WPF - 善用路由事件

    原文:WPF - 善用路由事件 在原来的公司中,编写自定义控件是常常遇到的任务.但这些控件常常拥有一个不怎么好的特点:无论是内部还是外部都没有使用路由事件.那我们应该怎样宰自定义控件开发中使用路由事件 ...

  9. WPF中自定义路由事件

    public class MyButtonSimple: Button { // Create a custom routed event by first registering a RoutedE ...

随机推荐

  1. metrics-server 安装问题解决

    参考:  https://www.qikqiak.com/post/install-metrics-server/   git clone https://github.com/kubernetes- ...

  2. 压缩,解压缩 和tar详细介绍

    文件压缩/解压缩  gzip   bzip2  xz 只能压缩文件,不能压缩文件夹(压缩完后,文件会消失) 先建三个文件来进行演示 touch ./{1..3}.txt  文件已经创建好,下面就开始介 ...

  3. Nginx应用详解及配置

    一.Nginx简介 概述:Nginx是一款由俄罗斯开发的开源的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务,其性能优势着为显著,官网上称:单台nginx服务器可以 ...

  4. Nginx 核心配置-location的登录账户认证实战篇

    Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...

  5. Nginx 核心配置-新建一个web站点

    Nginx 核心配置-新建一个web站点 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx基础配置常用参数说明 [root@node101.yinzhengjie.or ...

  6. JavaScript中数组去重汇总

    1. 简单的去重方法,利用数组的indexOf下标属性来查询 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ func ...

  7. 【Cookie】java.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value

    创建时间:6.30 java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie va ...

  8. django rest framework 解析器组件 接口设计,视图组件 (2)

    1. 使用视图组件进行接口优化 1.1 使用视图组件的mixin进行接口逻辑优化 - 导入mixin from rest_framework.mixinx import ( ListModelMix, ...

  9. wordpress迁移后登陆时出现Forbidden You don’t have permission to access /wp-login.php on this server

    之前在vps上,最近迁移到了php虚拟主机上,迁移后发现无法登陆后台出现403:Forbidden You don’t have permission to access /wp-login.php ...

  10. MMORPG服务器场景中的aoi算法思考

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lwtbn1/article/details/37961695 最近在做一个移动平台上的MMORPG项 ...