如何在MenuItemClick事件中取消选中Menuitem isCheked属性

时间:2013-07-11 09:49:34

标签: wpf c#-4.0

我可以检查菜单项但是无法取消选中,而且还有更多显示以前检查菜单项的内容。一旦我点击另一个菜单项,它就不会取消选中上一个菜单项。我正在从代码中添加menuitem。

  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个按钮应用相同的menuitems及其事件处理程序。所以,如果我为第一个按钮选择一个menuitem,它将显示menuitem上的检查,当我点击第二个按钮时出现选中的菜单项目。我可以实现这个目标?任何建议。

编辑:XAML

      <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添加menuitems的方式。

  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;
        }

1 个答案:

答案 0 :(得分:1)

不确定我明白你想要什么。但是如果你想要互相检查的MenuItems,你应该检查这篇文章: Mutually exclusive checkable menu items?