2011-03-24 88 views
0

如何使用C#添加一個WPF用戶控件庫到WPF窗口

xmlns:TechLog="clr-namespace:TechLog;assembly=TechLog" 

而且

   <TabItem Header="Technician"> 
       <TabControl> 

        <TabItem Header="System log"> 
         <TechLog:UserControl1></TechLog:UserControl1> 
        </TabItem> 

如何在WPF的cs文件做到這一點,而不是使用C#?我要動態地做到這一點,所以我加入他們的飛行

回答

2
using TechLog; 

... 

UserControl ctrl = new UserControl(); 
this.TabItem1.Content = UserControl; 
1

到TabControl添加一個名字:

<TabItem Header="Technician"> 
    <TabControl x:Name="tabCtrl" /> 
</TabItem> 

在代碼文件中:

添加使用:

using TechLog; 

添加標籤:

string header = "My tab header"; 
    var newTab = new TabItem() { Header = header }; 
    tabCtrl.Items.Add(newTab); 
+0

TabControl是一個ItemsControl,因此您可以使用ItemContainerGenerator從內容中生成標籤,但是在使用此方法時很難設置標頭。 – 2011-03-24 13:03:37

+0

它是如何將TabItems添加到TabControl?找到http://www.redmondpie.com/adding-tabitem-at-runtime-in-ac-wpf-application/但.items不存在 – Mech0z 2011-03-24 13:11:15

+0

TabItems將在TabControl.Items中,除非你正在做一些不尋常的事情與TabControl上的ItemsSource屬性。 – 2011-03-24 13:23:57