我有一个选项卡项,其内容是自定义用户控件。每当从选项卡控件中删除选项卡项时,我想通知主窗口。我想在 UserControl 中引发一个事件并让主窗口处理该事件。这是我的代码
在主窗口中:
public static readonly RoutedEvent CloseTabEvent = EventManager.RegisterRoutedEvent(
"CloseTab", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MainWindow));
private void CloseTab(object sender, RoutedEventArgs e)
{
Console.WriteLine("a tab item was removed");
}
在选项卡控件中:
RaiseEvent( new RoutedEventArgs(MainWindow.CloseTabEvent));
当我运行代码时,没有打印出“标签项被删除”。让它发挥作用的正确方法是什么?