我的 mainwindow.xaml 文件中有一个选项卡控件。在启动时,当应用程序运行时,会显示所有四个选项卡。有没有一种方法,一旦用户双击“连接”选项卡,该选项卡的整个视图就会弹出一个新的浮动窗口,如果我双击弹出的窗口,它会恢复到初始状态?
<Grid Grid.Row="0" >
<TabControl Name="ConnectTab" Style="{DynamicResource styleBackground}" />
<tablocal:CloseableTabItem Header="Connect" />
<tablocal:CloseableTabItem Header="I2C" />
<tablocal:CloseableTabItem Header="Voltage" />
<tablocal:CloseableTabItem Header="Clock" />
</TabControl>
</Grid>
这是我的 Tab 关闭按钮方法:
private void CloseTab(object source, RoutedEventArgs args)
{
TabItem tabItem = args.Source as TabItem;
if (ConnectTab != null && ConnectTab.Items.Count > 1)
ConnectTab.Items.Remove(tabItem);
}
Closetab 类:
public class CloseableTabItem : TabItem
{
static CloseableTabItem()
{
//This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
//This style is defined in themes\generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
}
public static readonly RoutedEvent CloseTabEvent =
EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CloseableTabItem));
public event RoutedEventHandler CloseTab
{
add { AddHandler(CloseTabEvent, value); }
remove { RemoveHandler(CloseTabEvent, value); }
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Button closeButton = base.GetTemplateChild("PART_Close") as Button;
if (closeButton != null)
closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
}
void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
}
}