2011-01-15 44 views

Trả lời

18

Mã này làm việc cho tôi:

public class DemoEventArgs : RoutedEventArgs 
{ 
    public DemoEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) 
    {} 
} 

public partial class TestControl : UserControl 
{ 
    public static readonly RoutedEvent DemoEvent = 
     EventManager.RegisterRoutedEvent(
      "Demo", 
      RoutingStrategy.Bubble, 
      typeof(RoutedEventHandler), 
      typeof(TestControl)); 

    public event RoutedEventHandler Demo 
    { 
     add { AddHandler(DemoEvent, value); } 
     remove { RemoveHandler(DemoEvent, value); } 
    } 

    public TestControl() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     RaiseEvent(new DemoEventArgs(TestControl.DemoEvent, sender)); 
    } 
} 

Sử dụng mã này, bạn có thể đăng ký tham gia sự kiện như thế này:

<Grid> 
    <StackPanel local:TestControl.Demo="TestControl_Demo" > 
     <local:TestControl /> 
    </StackPanel> 
</Grid> 
+0

Cảm ơn bạn rất nhiều, sẽ cố gắng tại văn phòng thứ hai. – user310291

+0

Liên kết đến tài liệu về nó tại MSDN: https://msdn.microsoft.com/en-us/library/ms752288(v=vs.110).aspx –

Các vấn đề liên quan