0

我能够检查菜单项,但无法取消选中,并且更多地显示之前在菜单项中的检查。一旦我单击另一个菜单项,它不会取消选中前一个菜单项。我正在从代码中添加菜单项。

  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 == "B1")
                {
                    btnMenu.Content = header;
                    item.IsChecked=true;
                }
                else if (header == "A1")
                {
                    btnMenu.Content = header;
                    item.IsChecked=true;
                }

            }

我正在为 4 个按钮应用相同的菜单项及其事件处理程序。因此,如果我为第一个按钮选择一个菜单项,它将显示对菜单项的检查,当我单击第二个按钮时会显示选中的菜单项。我该如何实现? ..任何建议。

编辑:xml

      <StackPanel Grid.Row="11" Orientation="Horizontal" FlowDirection="LeftToRight">
                                    <Button  Name="btnPhone" Content="Mobile" HorizontalAlignment="Left" VerticalAlignment="Top"  Margin="0 0 12 0" Width="85" Click="OnbtnPhoneClick"></Button>
                                                                          <Button   HorizontalAlignment="Left"  Name="ddBtnPhone" VerticalAlignment="Top" Width="25"  Click="OnddBtnPhoneClick"  >
                                        <Button.Content>
                                            <Path x:Name="btnArrow1" 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 x:Name="cmPhone">

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


                                </StackPanel>
                                <StackPanel Grid.Row="12" Orientation="Horizontal" FlowDirection="LeftToRight">
                                    <Button  Name="btnHome" Content="Business" HorizontalAlignment="Left"  Margin="0,0,12,5" Width="85" Click="OnbtnHomeClick" />
                                    <!--<extToolkit:DropDownButton x:Name="ddBtnHome" VerticalAlignment="Top" Width="30" HorizontalAlignment="Right" Margin="0 0 30 0" Height="20"/>-->
                                    <Button   HorizontalAlignment="Left"  Name="ddBtnHome" VerticalAlignment="Bottom" Width="25"  Click="OnddBtnHomeClick" Margin="0,0,0,5"  >
                                        <Button.Content>
                                            <Path x:Name="btnArrow2" 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="cmHome">

                                            </ContextMenu>
                                        </Button.ContextMenu>
                                    </Button>
                                </StackPanel>
                                <StackPanel Grid.Row="13" Orientation="Horizontal" FlowDirection="LeftToRight">
                                    <Button  Name="btnMobile" Content="Home" HorizontalAlignment="Left" VerticalAlignment="Top"  Margin="0 0 12 0" Width="85" Click="OnbtnMobileClick"></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="25" 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="cmMobile">

                                            </ContextMenu>
                                        </Button.ContextMenu>
                                    </Button>
                                </StackPanel>
                                <StackPanel Grid.Row="14" Orientation="Horizontal" FlowDirection="LeftToRight">
                                    <Button  Name="btnFAX" Content="Business FAX" HorizontalAlignment="Left" VerticalAlignment="Top"  Margin="0 0 12 0" Width="85" Click="OnbtnFAXClick"></Button>
                                    <!--<extToolkit:DropDownButton x:Name="ddBtnFAX" VerticalAlignment="Top" Width="30" HorizontalAlignment="Right" Margin="0 0 30 0" Height="20"/>-->
                                    <Button   HorizontalAlignment="Left" Name="ddBtnFAX" VerticalAlignment="Bottom" Width="25" Click="OnddBtnFAXClick"  Margin="0,0,0,5"  >
                                        <Button.Content>
                                            <Path x:Name="btnArrow4" 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="cmFAX">

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

                                </StackPanel>

这就是我为每个 contextMenu 添加菜单项的方式。

  private void AddMenuItems(ContextMenu cMenu, Button btnName)
    {
        if (cMenu.Items.Count == 0)
        {
            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 = "B2";
            cMenu.Items.Add(mnItem);
            mnItem = new MenuItem();
            mnItem.Header = "A2";
            cMenu.Items.Add(mnItem);

            cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
            cMenu.AddHandler(MenuItem.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseButtonUpEventClick));
            btnCommon = btnName;
        }
4

1 回答 1

1

不知道我明白你想要什么。但是,如果您想要检查互斥的 MenuItems,您应该查看这篇文章: 互斥的可检查菜单项?

于 2013-07-11T11:56:48.733 回答