1

我创建了一个用户控件,然后自定义了一个路由事件。目前,我遇到了一个问题,当第一次初始化时,它引发了无效,但其他时间可以正常工作。我确信该事件在初始化时引发,但引发无效,并且没有异常消息。

用户控制代码如下:

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);


            }
        }

    }

任何人都可以帮忙吗?谢谢。

4

0 回答 0