2012-05-15 19 views
0

我创建了以下用户控件:声明此路由事件有什么问题?

public partial class ReplacementPatternEditor : UserControl, INotifyPropertyChanged 
{ 
    .... 

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

    void RaiseCollectionChangedEvent() 
    { 
     RoutedEventArgs newEventArgs = new RoutedEventArgs(ReplacementPatternEditor.CollectionChanged); 
     RaiseEvent(newEventArgs); 
    } 

    ... 
} 

现在,当我尝试使用我的XAML代码这里面路由事件:

<local:ReplacementPatternEditor ItemsSource="{Binding MyItemSource}" CollectionChanged="OnCollectionChanged"/> 

我得到在编译以下错误:

The property 'CollectionChanged' does not exist in XML namespace 'clr-namespace:MyNamespace' 

为什么我得到这个,以及如何使路由事件工作?

回答

2

看着这个MSDN Link。它讨论了注册一个你已经完成的处理程序,然后讨论了如何在事件中提供CLR访问器,而在代码中没有。然后它添加事件处理程序。你不必事件声明

即是这样的

public static readonly RoutedEvent CollectionChangedEvent = EventManager.RegisterRoutedEvent( 
    "CollectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ReplacementPatternEditor)); 

public event RoutedEventHandler CollectionChanged 
{ 
    add { AddHandler(CollectionChangedEvent, value); } 
    remove { RemoveHandler(CollectionChangedEvent, value); } 
} 


void RaiseCollectionChangedEvent() 
{ 
    RoutedEventArgs newEventArgs = new RoutedEventArgs(ReplacementPatternEditor.CollectionChanged); 
    RaiseEvent(newEventArgs); 
}