简体   繁体   English

TabControl保存所选标签项已更改

[英]TabControl Saving on selected tab item changed

I need to save data in distinct TabItem every time when user switches to another tabitem. 每次用户切换到另一个tabitem时,我都需要在不同的TabItem保存数据。

I try to operate TabControl.SelectionChanged event, but there is no info about previously selected tab item. 我尝试操作TabControl.SelectionChanged事件,但没有关于以前选择的选项卡项的信息。

So, how to get moment when user switches from my TabItem to another? 那么,如何让用户从我的TabItem切换到另一个?

Use the Enter and Leave events of the individual tabs. 使用各个选项卡的Enter和Leave事件。 If you need the enter event to fire on code start up then you may need to programmatically change the selected tab to one that is different than at design time. 如果您需要在启动代码时触发输入事件,则可能需要以编程方式将所选选项卡更改为与设计时不同的选项卡。

You can make a global variable to store what is the last tab 您可以创建一个全局变量来存储最后一个选项卡

private TabPage LastTab = null;

private void tabSelectionChanged(...)
{
  if(LastTab != null)
     //Do save

  LastTab = tab.SelectedTabPage;// or equivalent 
}

Use the below code: 使用以下代码:

private object LastTab = null;

private void tabSelectionChanged(...)
{
  if(LastTab != null)
  {
     //Do save
  }

  LastTab = control.SelectedContent;
}

Here the the content will be of type object you can type cast to specific class and do the save operation 这里的内容将是类型对象,您可以键入强制转换为特定类并执行保存操作

您需要的参数SelectionChangedEventArgs e存在:

  • e.AddedItems
  • e.RemovedItems

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

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