這就是問題所在。我有一個窗口分成三個面板。中間包含一個繪圖表面,左側包含一個選項卡控件。選項卡控件的選項卡每個都包含一個必須在右側面板中打開新菜單的按鈕列表。我無法弄清楚如何在代碼中這樣做,所以我在C#中運行時分別創建了每個按鈕。似乎有必要有一個更好的方法去實現它。我目前調用下面的按鈕點擊事件函數來在運行時的右側面板中的TabControl中繪製不同的菜單。它需要一個字符串參數來指定要繪製的菜單集,儘管此時我只寫了一個菜單的代碼。以下是函數和xml的代碼。有沒有更好的方法來解決這個問題?運行時更新TabControl內容
XML:
<TabControl DockPanel.Dock="Right" Background="White" x:Name="tabctrl">
<TabItem Height ="38" Name="Tab1" Header="tab3"/>
</TabControl>
C#:
private void menuOpen(string menuSelected)
{
//Logic statement for what menu is being opened
switch (menuSelected)
{
case "BackGround":
{
//Remove Current Tabs
//Initialize Tab Item, set title, and add tab item to tab control
TabItem BackGround = new TabItem();
BackGround.Header = "BackGround";
tabctrl.Items.Insert(1, BackGround);
BackGround.Height = 38;
//Initialize Stack Panel, set orientation, and add to tab control
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Vertical;
BackGround.Content = panel;
//Initialize Menu Items
Button AddMap = new Button();
Button AddDemoMap = new Button();
Button RemoveMap = new Button();
Button MoveSelected = new Button();
Button Properties = new Button();
Button ScaleBackground = new Button();
//Define Button Text
AddMap.Content = "Add Map";
AddDemoMap.Content = "Add Demo Map";
RemoveMap.Content = "Remove Map";
MoveSelected.Content = "Move Selected Map to Top of List";
Properties.Content = "Properties";
ScaleBackground.Content = "Scale Background to Pipes";
AddMap.Height = 50;
AddDemoMap.Height = 50;
RemoveMap.Height = 50;
MoveSelected.Height = 50;
Properties.Height = 50;
ScaleBackground.Height = 50;
//Add Buttons to StackPanel
panel.Children.Add(AddMap);
panel.Children.Add(AddDemoMap);
panel.Children.Add(RemoveMap);
panel.Children.Add(MoveSelected);
panel.Children.Add(Properties);
panel.Children.Add(ScaleBackground);
}
break;
如果你發佈你想要的畫面是什麼樣子的截圖這將有助於。 – 2013-03-04 20:17:49