public class MyButtonSimple: Button
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
"Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple)); // Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
} // This method raises the Tap event
void RaiseTapEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
RaiseEvent(newEventArgs);
}
// For demonstration purposes we raise the event when the MyButtonSimple is clicked
protected override void OnClick()
{
RaiseTapEvent();
} }
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:SDKSample;assembly=SDKSampleLibrary"
x:Class="SDKSample.RoutedEventCustomApp" >
<Window.Resources>
<Style TargetType="{x:Type custom:MyButtonSimple}">
<Setter Property="Height" Value="20"/>
<Setter Property="Width" Value="250"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Background" Value="#808080"/>
</Style>
</Window.Resources>
<StackPanel Background="LightGray">
<custom:MyButtonSimple Name="mybtnsimple" Tap="TapHandler">Click to see Tap custom event work</custom:MyButtonSimple>
</StackPanel>
</Window>

备注:在MVVM模式中,不能直接绑定控件的路由事件到ViewModel,可以将事件绑定后台.cs中的方法,然后再调用ViewModel中的方法。

.cs文件:

private void TapHandler(object sender, RoutedEventArgs e)
{
ViewModel vm = this.DataContext;
vm.method;
}

WPF中自定义路由事件的更多相关文章

  1. WPF中的路由事件(转)

    出处:https://www.cnblogs.com/JerryWang1991/archive/2013/03/29/2981103.html 最近因为工作需要学习WPF方面的知识,因为以前只关注的 ...

  2. WPF 中的 路由事件

    public class ReportTimeEventArgs:RoutedEventArgs { public ReportTimeEventArgs(RoutedEvent routedEven ...

  3. WPF自定义路由事件(二)

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

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

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

  5. WPF:自定义路由事件的实现

    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...

  6. WPF自学入门(四)WPF路由事件之自定义路由事件

    在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ...

  7. WPF路由事件三:自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...

  8. WPF 自定义路由事件

    如何:创建自定义路由事件 首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent ...

  9. Wpf自定义路由事件

    创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ...

随机推荐

  1. MySQL加载配置文件的顺序

    MySQL5.6启动时,按照下表,从上往下的顺序加载配置文件: File Name Purpose /etc/my.cnf Global options /etc/mysql/my.cnf Globa ...

  2. 跟我学SharePoint 2013视频培训课程——删除恢复、文档离线工作(11)

    课程简介 第11天,怎样在SharePoint 2013中删除.恢复文档.文档离线工作. 视频 SharePoint 2013 交流群 41032413

  3. python 多进程,实际上都没有运行,sleep

    进程以及状态 1. 进程程序:例如xxx.py这是程序,是一个静态的 进程:一个程序运行起来后,代码+用到的资源 称之为进程,它是操作系统分配资源的基本单元. 不仅可以通过线程完成多任务,进程也是可以 ...

  4. [转]What are mode and status columns under gp_segment_configuration table

    February 16, 2017 10:39 Goal In this article we will try to understand and answer to the below two q ...

  5. talend hive数据导入到mysql中

    thiveInput->tmap->tMysqloutput thiveInput: tmap: tmysqlOutput:注意编码问题:noDatetimeStringSync=true ...

  6. ubuntu 安装 2.10.x版本的scala

    Ubuntu 14.04.1 LTS上默认的scala版本是2.9的,而最新版本的spark-1.3需要最低版本的scala版本为2.10.x,先使用apt-get remove scala将机器上的 ...

  7. Jenkins管理静态资源

    这里我们的前端是使用webpack来管理静态资源的,把静态资源上传到svn上面来管理 这里我们把项目和静态资源剥离开来,然后静态资源接入CDN 我们的svn的结构是这样的 我们需要把这些目录都进行打包 ...

  8. 我用Xamarin开发android应用,应用在真机上一打开就退出了

    在解决方案管理器的项目上右键--属性--Android Options--Packaging将Use Shared Runtime前面的对勾取消即可.

  9. Atitit gui界面ui技术发展史与未来趋势

    Atitit gui界面ui技术发展史与未来趋势 1. Gui技术的发展,从像素自绘到native控件体系,再到dsl h51 1.1. 编程语言的发展 从机器语言,汇编语言到本地native语言(c ...

  10. Windows 以管理员运行而不提示

    组策略中设置一下:“计算机配置”-“Windows 配置”-“安全设置”-“本地策略”-“安全选项”下:修改“用户帐户控制: 在管理审批模式下管理员的提升提示行为”选项为“提示凭据”就会再弹出提示框了 ...