原文:整理: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. MySQL主从仅同步指定库

    有两种方式,1.在主库上指定主库二进制日志记录的库或忽略的库: vim /etc/my.cnf ... binlog-do-db=xxxx 二进制日志记录的数据库 binlog-ignore-db=x ...

  2. Chrome无界面启动使用

    Method1: from selenium import webdriver # 创建chrome参数对象opt = webdriver.ChromeOptions() # 把chrome设置成无界 ...

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

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

  4. 大数据技术原理与应用【第五讲】NoSQL数据库:5.3 NoSQL的四大类型

    5.3 NoSQL的四大类型   5.3.1 键值数据库和列族数据库 可以分为四大类产品:键值数据库,列族数据库,文档数据库,图数据库 (代表)   1.键值数据库:   用的多:redis云数据库: ...

  5. vue-router编程式跳转

    除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现. [语法] .

  6. WeUI教程/第三方扩展及其他UI框架对比

    WeUI 是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信内网页和微信小程序量身设计,令用户的使用感知更加统一.包含button.cell.dialog. progress. toa ...

  7. 第六章深入python的set和dict

    1.collections中的abc MutableMapping是Mapping的子类 Mapping是Collection的子类 Collection是Sized,Iterable,Contain ...

  8. [RN] React Native 自定义导航栏随滚动渐变

    React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...

  9. selenium--更改标签的属性值

    前戏 在进行web自动化的时候,我们有时需要获取元素的属性,有时需要添加,有时需要删除,这时候就要通过js来进行操作了 实战 from selenium import webdriver import ...

  10. 《Machine Learning - 李宏毅》视频笔记(完结)

    https://www.youtube.com/watch?v=CXgbekl66jc&list=PLJV_el3uVTsPy9oCRY30oBPNLCo89yu49 https://www. ...