首先
1.window 不能添加到其他控件中,原因是他是最高级的。。
在window usercontrol,以及page,frame 基本都遵循这个道理,可以添加的则是 除window以外的其他窗体。
2.添加到TabControl 下面的TabItem 控件添加usercontrl我遇到的问题是,控件是有window控件手动转成usercontrol 其中有个参数是错误的,无法试下自适应大小:后来手动修改即可
而window下面的操作是:
差别还是挺大的。
3.适用的方法:
public static bool AddTabcontrol(List<string> TabKeys, TabControl ThisTBC, UserControl ThisWindows)
{
string TagInfo = ThisWindows.Tag.ToString();
string[] ThisTagShowArr = TagInfo.Split(',');
if (ThisTagShowArr.Length == 2)
{
string ThisKey = ThisTagShowArr[0];
string NamaPath = ThisTagShowArr[1];
if (!TabKeys.Contains(ThisKey))
{
TabItem ThisItem = new TabItem();
ThisItem.Header= ThisKey;
ThisItem.Name = ThisKey;
/* ThisItem.Content= ThisWindows; */ //Uri MainTragetUri = new Uri(NamaPath, UriKind.RelativeOrAbsolute);
//Frame ThisFrm = new Frame();
//ThisFrm.Name = "frameMaim";
//ThisFrm.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
//ThisFrm.JournalOwnership = System.Windows.Navigation.JournalOwnership.UsesParentJournal;
//ThisFrm.Source = MainTragetUri;
//ThisFrm.Navigate(ThisWindows);
ThisItem.Content = ThisWindows;
ThisTBC.Items.Add(ThisItem);
ThisTBC.SelectedItem = ThisItem;
TabKeys.Add(ThisKey);
}
else
{
int IndexNo = TabKeys.IndexOf(ThisKey);
foreach (TabItem OneItem in ThisTBC.Items)
{
if (OneItem.Name == ThisKey)
{
ThisTBC.SelectedIndex = IndexNo;
}
}
}
return true;
}
else
{
return false;
}
}
4.本数据是从网上抄袭下来的:
public List<TabItem> tabItemsList = new List<TabItem>();
public TabControl tabDynamic = null;
tabDynamic = queryControl.GetChildObject<System.Windows.Controls.TabControl>(layOut, "tabDynamic"); //获取TabControl 控件
public void AddTabItem(string uriName)
{
int count = tabItemsList.Count;
// create new tab item
TabItem tab = new TabItem();
tab.Header = string.Format("Tab {0}", uriName.Split('/')[1]);
tab.Name = string.Format("tab{0}", uriName.Split('/')[1]);
tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
//tab.Background = new SolidColorBrush(Colors.Transparent);
//tab.MouseDoubleClick += new MouseButtonEventHandler(tab_MouseDoubleClick);
Uri MainTragetUri = new Uri(string.Format("/Freed.Api.Monitor;component/View/{0}.xaml", uriName), UriKind.RelativeOrAbsolute);
Frame frame = new Frame();
frame.Name = "frameMaim";
frame.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
frame.JournalOwnership = System.Windows.Navigation.JournalOwnership.UsesParentJournal;
frame.Source = MainTragetUri;
tab.Content = frame;
// insert tab item right before the last (+) tab item
var tabOld = (from t in tabItemsList where t.Name == tab.Name select t).FirstOrDefault();
if (tabOld == null)
{
if (count > 0)
{
tabItemsList.Insert(count - 1, tab);
}
else
{
tabItemsList.Add(tab);
}
tabDynamic.SelectedItem = tab;
}
else
{
tabDynamic.SelectedItem = tabOld;
}
}