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'

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

4

1 回答 1

2

看着这个MSDN 链接。它讨论了注册您已经完成的处理程序,然后讨论了为我在您的代码中看不到的事件提供 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); 
} 
于 2012-05-15T21:17:53.423 回答