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附加方法ide

<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包含引起事件的對象spa

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# 編程指南)code

  1. 接口可聲明 事件。在類中實現接口事件:在類中聲明事件,而後在適當的區域調用該事件。
  2. 不常見狀況:您的類是從兩個以上的接口繼承的,每一個接口都含有同名事件)。 在這種狀況下,您至少要爲其中一個事件提供顯式接口實現。 爲事件編寫顯式接口實現時,必須編寫 add 和 remove 事件訪問器。 這兩個事件訪問器一般由編譯器提供,但在這種狀況下編譯器不能提供。
  3. 您能夠提供本身的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,仍是由不一樣事件表示。 例如,根據接口規範,若是事件應在不一樣時間引起,則能夠將每一個事件與類中的一個單獨實現關聯(實際就是在同一個方法中,先後順序引起不一樣的兩個事件)。

RoutedEventArgs 類:包含與路由事件相關的狀態信息和事件數據。對象

  1. 能夠爲單個 RoutedEvent 使用不一樣的 RoutedEventArgs。
  2. 此類負責打包 RoutedEvent 的事件數據和提供額外的事件狀態信息,而且,事件系統將使用此類來調用與路由事件關聯的處理程序。
相關文章
相關標籤/搜索