简体   繁体   English

WPF UserControl是tabcontrol中的一个标签-如何检测它是否正在关闭

[英]WPF UserControl that is a tab in a tabcontrol - how to detect if it is closing

I have a class that is inheriting from a UserControl. 我有一个从UserControl继承的类。 I am showing this class in a WPF TabControl as a tab. 我在WPF TabControl中将此类显示为选项卡。 The tab has a small x and can be closed by clicking on it. 该选项卡带有一个小的x,可以通过单击将其关闭。 I need a way to do some cleanup code before the tab is destroyed. 我需要一种方法来删除标签之前执行一些清理代码。

I don't believe I can use the Unloaded event to do this because the Unloaded event is called when the UserControl is being destroyed and it is also being called when you click on another tab. 我不相信我可以使用Unloaded事件来执行此操作,因为在销毁UserControl时会调用Unloaded事件,而在单击另一个选项卡时也会被调用。

Any ideas on how to deal with this situation? 关于如何处理这种情况有什么想法?

EDIT: Here is more info. 编辑:这是更多信息。 In my UserControl class I have a 3rd part control that I am using. 在我的UserControl类中,我有一个正在使用的第三部分控件。 Basically a graphing control. 基本上是图形控件。 There are a couple lines of code I would like to run to ensure that there are no memory leaks. 我想运行几行代码以确保没有内存泄漏。 If you want to read more about it then this would be the web address that talks more about it: 如果您想阅读更多有关此的内容,那么这将是更多有关此内容的网址:

http://support.scichart.com/index.php?/News/NewsItem/View/21/wpf-xname-memory-leak--how-to-clear-memory-in-scichart http://support.scichart.com/index.php?/News/NewsItem/View/21/wpf-xname-memory-leak--how-to-clear-memory-in-scichart

You can have a look at the way I have done this in the dragablz TabControl on GitHub . 您可以在GitHub上dragablz TabControl中查看我完成此操作的方式。

Essentially the TabControl listens to a RoutedCommend raised from the close button, then calls an optional callback which enables a MVVM view model (or old-style control type code) to dispose an associated view model, or perform any other tidy up code you want to do (or indeed cancel the close operation). 从本质上讲,TabControl会侦听从关闭按钮引发的RoutedCommend,然后调用可选的回调,该回调使MVVM视图模型(或旧式控件类型代码)能够处理关联的视图模型,或执行您要整理的任何其他整理代码做(或实际上取消关闭操作)。

In the example project file on GitHub, look for ClosingItemHandlerImpl and work back from there. 在GitHub上的示例项目文件中,查找ClosingItemHandlerImpl并从那里开始工作。

http://github.com/ButchersBoy/Dragablz/blob/master/DragablzDemo/BoundExampleModel.cs http://github.com/ButchersBoy/Dragablz/blob/master/DragablzDemo/BoundExampleModel.cs

ClosingItemHandlerImpl is bound in from the XAML, and the tab control will call it prior to removing a tab. 从XAML绑定ClosingItemHandlerImpl,并且在删除选项卡之前,选项卡控件将对其进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM