Brief introduction
In the previous article, we used Avalondock to implement a tab similar to VS (or a browser's tab) effect. But we are implementing it through XAML code.
Now we are trying to implement the dynamic addition of the tab through C # code.
modifying XAML code
In the XAML code for the previous article, add the following code:
<grid.rowdefinitions> <rowdefinition Height="+"></rowdefinition> <rowdefinition></rowdefinition> </grid.rowdefinitions> <menu Name="Menu1"grid.row="0"> <MenuItem Name="Item_addnew" Header="Add new tab" click ="Item_addnew_click"></MenuItem> </Menu>
In XAML code, add the Name property to Layoutdocumentpane.
<avalon:LayoutDocumentPane x:Name="layOutPane" DockWidth="300">
In our design, we do the work of dynamically adding tabs by clicking on the "Add New tab" menu. As a result, dynamically added C # code needs to be written in Item_addnew_click.
Write C # code for dynamic Additions
Open the MainWindow.xaml.cs file and add the following reference to the code header:
using Xceed.Wpf.AvalonDock;using Xceed.Wpf.AvalonDock.Layout;
Then write the menu click events.
int0; privatevoiditem_AddNew_Click(object sender, RoutedEventArgs e) { clickCount++; new"新选项卡"+clickCount }; new TextBox() { Text="这是第"+clickCount+"个新选项卡"}; layOutPane.Children.Add(layOutAnc); }
To run the program, the effect is as follows:
We have implemented the dynamic addition of tabs.
WPF Implementation tab Effects (2)--Dynamic Add avalondock tab