How to dynamically create a tabcontrol item

Source: Internet
Author: User

Http://www.cnblogs.com/salam/archive/2010/07/14/1777215.html

 

 

<Usercontrol xmlns: SDK = "CLR-namespace: system. Windows. controls; Assembly = system. Windows. Controls" X: class = "Silverlight. Common. View. tabcontroldemo"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D"
D: designheight = "300" D: designwidth = "400" xmlns: Toolkit = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

<Grid X: Name = "layoutroot" background = "white">
<Grid. rowdefinitions>
<Rowdefinition Height = "70"/>
<Rowdefinition Height = "Auto"/>
</Grid. rowdefinitions>
<Grid. columndefinitions>
<Columndefinition width = "100" type = "codeph" text = "/codeph"/>
<Columndefinition width = "Auto"/>
</Grid. columndefinitions>
<Stackpanel grid. Row = "1">

<Textblock margin = "5" X: Name = "DataGrid" text = "DataGrid" mouseleftbuttondown = "datagrid_mouseleftbuttondown"> </textblock>
<Textblock margin = "5" X: Name = "Treeview" text = "Treeview" mouseleftbuttondown = "treeview_mouseleftbuttondown"> </textblock>
<Textblock margin = "5" X: Name = "dataform" text = "dataform" mouseleftbuttondown = "dataform_mouseleftbuttondown"> </textblock>

</Stackpanel>
<SDK: tabcontrol borderthickness = "5" borderbrush = "green" minheight = "500" minwidth = "500" grid. column = "1" grid. row = "1" X: Name = "tabcontrol">

</SDK: tabcontrol>

</GRID>
</Usercontrol>

 

 

2. tabcontroldemo. CS

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;

Namespace Silverlight. Common. View
{
Public partial class tabcontroldemo: usercontrol
{
Public tabcontroldemo ()
{
Initializecomponent ();
}

 

// Header is the title of tabitem, and typename is the full name of the displayed interface (namespace + class name)

Private void addtabitem (string header, string typename)
{
If (string. isnullorempty (header) | string. isnullorempty (typename ))
{
Return;
}

// Obtain the displayed tabitem and use the header of the tabitem as the search condition.

VaR list = This. tabcontrol. Items. Where (W => (tabitem) W). header. tostring () = header );

// If the clicked form has been opened, this form is displayed to avoid opening multiple identical forms.

If (list. Count ()> 0)
{
(Tabitem) List. First (). Visibility = visibility. visible;

This. tabcontrol. selecteditem = List. First ();
}
Else
{
Tabitem = new tabitem ();

Tabitem. header = header;

// Use the launch to instantiate a form class
VaR tabcontent = activator. createinstance (type. GetType (typename) as usercontrol;

Tabitem. content = tabcontent;

// Add the instantiated tabitem to tabcontrol
This. tabcontrol. Items. Add (tabitem );
This. tabcontrol. selecteditem = tabitem;
}
}

Private void datagrid_mouseleftbuttondown (Object sender, mousebuttoneventargs E)
{

// Silverlight. Common. View. datagriddemo is the datagriddemo (DataGrid Control) in the DataGrid Control Section of Silverlight learning notes 7)
This. addtabitem ("data", "Silverlight. Common. View. datagriddemo ");
}

Private void treeview_mouseleftbuttondown (Object sender, mousebuttoneventargs E)
{

// Treeviewsample (Treeview control) in the Treeview control of Silverlight learning notes 6)
This. addtabitem ("Tree View", "Silverlight. Common. View. treeviewsample ");
}

Private void dataform_mouseleftbuttondown (Object sender, mousebuttoneventargs E)
{

// Silverlight study note 7 datapager, dataform control dataform ()
This. addtabitem ("Data Form", "Silverlight. Common. View. dataformdemo ");
}
}
}

 

 

 

Note: source code download: http://files.cnblogs.com/salam/Silverlight.Common.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.