ContextMenu MenuItems在Click事件上不断添加

时间:2013-07-08 11:25:03

标签: wpf c#-4.0

在点击事件上,我已经将菜单项添加到contextmenu.but不止一次点击它继续将菜单项添加到contextmenu。以下代码正在使用它。

<StackPanel Grid.Row="13" Orientation="Horizontal" FlowDirection="LeftToRight">
                                    <Button  Name="btnMobile" Content="Home" HorizontalAlignment="Left" VerticalAlignment="Top"  Margin="0 0 20 0" Width="70"></Button>
                                    <!--<extToolkit:DropDownButton x:Name="ddBtnMobile" VerticalAlignment="Top" Width="30" HorizontalAlignment="Right" Margin="0 0 30 0" Height="20"/>-->
                                    <Button   HorizontalAlignment="Left" Name="ddBtnMobile" Width="30" Click="OnddBtnMobileClick"  Margin="0,0,0,5"  >
                                        <Button.Content>
                                            <Path x:Name="btnArrow3" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
                                        </Button.Content>
                                        <Button.ContextMenu>
                                            <ContextMenu Name="cMenu">

                                            </ContextMenu>
                                        </Button.ContextMenu>
                                    </Button>
                                </StackPanel>

使用的代码在

下面
     private void OnddBtnMobileClick(object sender, RoutedEventArgs e)
                {
                    mnItem = new MenuItem();
                    mnItem.Header ="B1"; 
                    cMenu.Items.Add(mnItem);
                    mnItem = new MenuItem();
                    mnItem.Header ="A1";
                    cMenu.Items.Add(mnItem);
                    mnItem = new MenuItem();
                    mnItem.Header="B 2";
                    cMenu.Items.Add(mnItem);
 cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
                }

  private void OnMenuItemClick(object sender, RoutedEventArgs e)
                {
                    RoutedEventArgs args = e as RoutedEventArgs;
                    MenuItem item = args.OriginalSource as MenuItem;
                    string header = item.Header.ToString();
                    if (header == "Business")
                    {
                        btnMobile.Content = header;
                    }
                    else if (header == "Assistant")
                    {
                        btnMobile.Content = header;
                    }

                }

如何解决我的问题..有没有更好的方法来编写上述逻辑。即,在运行时添加上下文菜单的菜单项。

2 个答案:

答案 0 :(得分:0)

在按钮点击事件的开始中添加以下代码。

cMenu = new cMenu();

你需要创建一个新实例。

谢谢,

答案 1 :(得分:0)

添加一个布尔数据成员,用于检查子菜单是否已添加

private void OnddBtnMobileClick(object sender, RoutedEventArgs e)
{
   if(alreadyAdded == true) 
      return;

   alreadyAdded = true;
   mnItem = new MenuItem();
   mnItem.Header ="B1"; 
   cMenu.Items.Add(mnItem);
   mnItem = new MenuItem();
   mnItem.Header ="A1";
   cMenu.Items.Add(mnItem);
   mnItem = new MenuItem();
   mnItem.Header="B 2";
   cMenu.Items.Add(mnItem);
   cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
}