如何将事件处理程序从自定义控件附加到其父用户控件关闭事件?

How to attach an eventhandler from a customcontrol to its parent usercontrol close event?

我一直找不到这个问题的答案。我有一长串用户控件,每个控件都创建一个或多个子用户控件。该系列中的最后一个用户控件创建了一个使用非托管代码的自定义控件。

我需要让自定义控件知道用户在单击右上角 "X" 时关闭其父用户控件 window。这样才能正确处理非托管代码。

基本的XAML是:

 <UserControl x:Class="Nova5.UI.Views.Ink.InkEditorView"
      <!--DataContext in InkEditorViewModel.cs-->
      <wc:CustomInkCanvas x:Name="myInkCanvas"
                          Vocabulary="{Binding Vocabulary, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                          Text="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
                          WordPad="{Binding WordPad, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
                          CloseCharacterPads ="{Binding CloseCharacterPads}"/>
</UserControl>

假设用户控件本身可以知道来自其父用户控件的关闭事件,我如何将自定义控件 wc:CustomInkCanvas 中的方法附加到其父用户控件的关闭事件?

非常感谢任何帮助。谢谢

这不是我真正希望得到的答案,但是当放在 customInkCanvas 构造函数中时它会起作用:

  Unloaded += new RoutedEventHandler(CustomInkCanvas_Unloaded);

有更好的主意吗?