所以这就是问题所在。我有一个窗口分为三个面板。中间包含一个绘图表面,左侧包含一个选项卡控件。选项卡控件的每个选项卡都包含一个必须在右侧面板中打开新菜单的按钮列表。我无法弄清楚如何在代码中执行此操作,因此我求助于在 C# 运行时单独创建每个按钮。似乎必须有更好的方法来解决它。我目前为按钮单击事件调用以下函数,以在运行时在名为“tabctrl”的右侧面板中绘制不同的菜单。它需要一个字符串参数来指定要绘制的菜单集,尽管此时我只为一个菜单编写了代码。下面是函数和xml的代码。有没有更好的方法来解决这个问题?
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;