我创建了一个UserControl,然后自定义了routedEvent.Currently,我遇到了一个问题,当第一次初始化时,它引发了无效,但是时间可能正常。 我确信在init处引发了事件,但是引发了无效,并且没有异常消息。
下面的UserControl代码:
BaseDictionaryUpDownCheckListBox.xaml.cs
static BaseDictionaryUpDownCheckListBox()
{
//customize a routedEvent
SelectedItemsChangedEvent =
EventManager.RegisterRoutedEvent("SelectedItemsChanged", RoutingStrategy.Bubble,
typeof (EventHandler<RoutedEventArgs>)
, typeof (BaseDictionaryUpDownCheckListBox));
}
//customize a routedEvent
public static readonly RoutedEvent SelectedItemsChangedEvent;
public event RoutedEventHandler SelectedItemsChanged
{
add { this.AddHandler(BaseDictionaryUpDownCheckListBox.SelectedItemsChangedEvent, value); }
remove { this.RemoveHandler(BaseDictionaryUpDownCheckListBox.SelectedItemsChangedEvent, value); }
}
public BaseDictionaryUpDownCheckListBox()
{
BaseDictionaryUpDownCheckListBoxViewModel = new BaseDictionaryUpDownCheckListBoxViewModel();
InitializeComponent();
LayoutRoot.DataContext = BaseDictionaryUpDownCheckListBoxViewModel;
BaseDictionaryUpDownCheckListBoxViewModel.PropertyChanged += PropertyChangedByDetailViewModel;
}
private void PropertyChangedByDetailViewModel(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedItems")
{
var detailViewModel = sender as BaseDictionaryUpDownCheckListBoxViewModel;
if (detailViewModel != null)
{
SelectedItems = detailViewModel.SelectedItems;
RoutedEventArgs routedArgs =
new RoutedEventArgs(BaseDictionaryUpDownCheckListBox.SelectedItemsChangedEvent, this);
//I was sure that event raised at init,but raised invalid,and no exception Messages
base.RaiseEvent(routedArgs);
}
}
}
有人可以帮忙吗? 感谢。