2010-06-18 58 views
9

標題幾乎解釋了這個問題。當應用程序第一次運行時,我有一個用戶控件加載到主窗口中。我想要做的是在用戶控件上的按鈕被點擊時在父窗口上引發一個事件,那麼如何從用戶控件的button1_Click上引發父事件?在.NET C中的用戶控件上提升父窗口上的事件#

+0

作爲一個僅供參考,我正在寫一個WPF桌面應用程序,因此冒泡用戶控件事件的例子將是完美的。 – user246392 2010-06-18 06:32:52

回答

2

你需要一個RoutedEvent link

「路由事件是其向上或向下導航可視化樹acording他們RoutingStrategy事件。路由策略可以是泡沫,隧道或直接,你可以掛鉤的事件處理程序通過使用附加的事件語法引發事件或其他元素上方或下方的元素:Button.Click =「Button_Click」。「

29

這個問題似乎沒有得到充分描述終端到終端的任何地方:以上所提供的鏈接是如何定義,提出一個完整的解釋,並從用戶控件的父窗口的事件。對某些人來說,這可能是顯而易見的,但花了我一分鐘來連接點,所以我會分享我的發現。是的,要做到這一點的方法是通過將一個Custom RoutedEvent設置爲RoutingStrategy.Bubble,但這個故事更多。


在這個例子中,我有一個UserControl,名爲ServerConfigs,它有一個保存按鈕。當用戶點擊保存按鈕時,我需要一個父窗口上的按鈕,其中包含UserControl以啓用另一個按鈕,該按鈕稱爲btn_synchronize。

在後面的UserControl代碼中,根據上面引用的上面的RoutedEvent鏈接定義以下內容。

public partial class ServerConfigs : UserControl 
{ 

    // Create RoutedEvent 
    // This creates a static property on the UserControl, SettingsConfirmedEvent, which 
    // will be used by the Window, or any control up the Visual Tree, that wants to 
    // handle the event. This is the Custom Routed Event for more info on the 
    // RegisterRoutedEvent method 
    // https://msdn.microsoft.com/en-us/library/ms597876(v=vs.100).aspx 

    public static readonly RoutedEvent SettingConfirmedEvent = 
     EventManager.RegisterRoutedEvent("SettingConfirmedEvent", RoutingStrategy.Bubble, 
     typeof(RoutedEventHandler), typeof(ServerConfigs)); 

    // Create RoutedEventHandler 
    // This adds the Custom Routed Event to the WPF Event System and allows it to be 
    // accessed as a property from within xaml if you so desire 

    public event RoutedEventHandler SettingConfirmed 
    { 
     add { AddHandler(SettingConfirmedEvent, value); } 
     remove { RemoveHandler(SettingConfirmedEvent, value); } 
    } 
    .... 

    // When the Save button on the User Control is clicked, use RaiseEvent to fire the 
    // Custom Routed Event 

    private void btnSave_Click(object sender, RoutedEventArgs e) 
    { 
    .... 
     // Raise the custom routed event, this fires the event from the UserControl 
     RaiseEvent(new RoutedEventArgs(ServerConfigs.SettingConfirmedEvent)); 
    .... 
    } 
} 

有一個實現的例子,上面的教程結束。那麼,人們如何抓住窗戶上的事件並處理它呢?

從window.xaml,這是如何使用用戶控件中定義的Custom RoutedEventHandler。這酷似button.click等

<Window> 
.... 
<uc1:ServerConfigs SettingConfirmed="Window_UserControl_SettingConfirmedEventHandlerMethod" 
        x:Name="ucServerConfigs" ></uc1:ServerConfigs> 
... 
</Window> 

或窗口後面的代碼,你可以通過設置在AddHandler的事件處理程序,一般在構造函數中:

public Window() 
{ 
    InitializeComponent(); 
    // Register the Bubble Event Handler 
    AddHandler(ServerConfigs.SettingConfirmedEvent, 
     new RoutedEventHandler(Window_UserControl_SettingConfirmedEventHandlerMethod)); 
} 

private void Window_UserControl_SettingConfirmedEventHandlerMethod(object sender, 
       RoutedEventArgs e) 
{ 
    btn_synchronize.IsEnabled = true; 
} 
+2

這篇文章值得一讀。 – 2014-02-28 14:42:47

+1

最後能夠讓RoutedEvents'點擊' - 這個答案包含了從頭到尾的整個過程。好東西。 – nocarrier 2016-03-15 01:04:17

相關問題