WPF:Events事件

AddingEventHandler添加事件句柄

clipboard.png

其原理的就是在处理初始button元素的点击事件方法时,同时委托方法到新建项button的点击事件上。

private void MakeButton(object sender, RoutedEventArgs e)
{
    var b2 = new Button {Content = "New Button"};
    // Associate event handler to the button. You can remove the event 
    // handler using "-=" syntax rather than "+=".
    b2.Click += Onb2Click;
    root.Children.Insert(root.Children.Count, b2);
    DockPanel.SetDock(b2, Dock.Top);
    text1.Text = "Now click the second button...";
    b1.IsEnabled = false;
}

CustomRoutedEvents自定义路由事件

clipboard.png

界面自定义button样式设计及自定义路由事件Tap附加方法

<Window.Resources>
    <Style TargetType="{x:Type local: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 VerticalAlignment="Center" HorizontalAlignment="Center" Background="LightGray">
    <local:MyButtonSimple x:Name="mybtnsimple" Tap="TapHandler">Click to see Tap custom event work</local:MyButtonSimple>
</StackPanel>

自定义路由事件Tap:

  1. EventManager.RegisterRoutedEvent()注册路由事件名称Tap,冒泡策略、路由句柄类型RoutedEventHandler,适用本类MyButtonSimple类型
  2. 提供CLR对事件的包装,在CLR事件访问器中add/remove,使用UIElement.AddHandler():为指定的路由事件添加路由事件处理程序,并将该处理程序添加到当前元素的处理程序集合中。
  3. 引发事件,重写OnClick方法,执行引发事件的方法RaiseTapEvent();RaiseTapEvent中初始化路由事件参数Args(TapEvent),使用UIElement.RaiseEvent(e);
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
    private void RaiseTapEvent()
    {
        var newEventArgs = new RoutedEventArgs(TapEvent);
        RaiseEvent(newEventArgs);
    }

    // For demonstration purposes we raise the event when the MyButtonSimple is clicked
    protected override void OnClick()
    {
        RaiseTapEvent();
    }
}

FindingSourceElement寻找事件引发源元素

clipboard.png

很简单的使用路由参数e,其Source包含引发事件的对象

private void HandleClick(object sender, RoutedEventArgs e)
{
    // You must cast the sender object as a Button element, or at least as FrameworkElement, to set Width
    var srcButton = e.Source as Button;
    srcButton.Width = 200;
}

ps:OriginalSource: 在父类进行任何可能的 Source 调整之前,获取原始报告源(由纯粹命中测试确定)。

RoutedEventHandling路由事件处理过程

clipboard.png

  1. 按钮的点击事件 被放于容器StackPanel中,其为处理事件的处理者send。
  2. e.Source为事件的引发者。
  3. e.OriginalSource为命中测试的元素引发者,比如button里面包含的图像,点击事件的 命中源伟图像,引发源为按钮。如不包含内容,一般命中源和引发者为同一个元素
<StackPanel Name="myStackPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Button.Click="HandleClick">
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Height" Value="20"/>
            <Setter Property="Width" Value="250"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
    </StackPanel.Resources>
    <Button Name="Button1">Item 1</Button>
    <Button Name="Button2">Item 2</Button>
    <TextBlock Name="results"/>
</StackPanel>

扩展:
如何:实现接口事件(C# 编程指南)

  1. 接口可声明 事件。在类中实现接口事件:在类中声明事件,然后在适当的区域调用该事件。
  2. 不常见情况:您的类是从两个以上的接口继承的,每个接口都含有同名事件)。 在这种情况下,您至少要为其中一个事件提供显式接口实现。 为事件编写显式接口实现时,必须编写 add 和 remove 事件访问器。 这两个事件访问器通常由编译器提供,但在这种情况下编译器不能提供。
  3. 您可以提供自己的访问器,以便指定这两个事件是由您的类中的同一事件表示,还是由不同事件表示。 例如,根据接口规范,如果事件应在不同时间引发,则可以将每个事件与类中的一个单独实现关联(实际就是在同一个方法中,前后顺序引发不同的两个事件)。

RoutedEventArgs 类:包含与路由事件相关的状态信息和事件数据。

  1. 可以为单个 RoutedEvent 使用不同的 RoutedEventArgs。
  2. 此类负责打包 RoutedEvent 的事件数据和提供额外的事件状态信息,并且,事件系统将使用此类来调用与路由事件关联的处理程序。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值