WPF MenuItem Click事件被调用两次,而不是一次

我的XAML中有一个看起来像这样的菜单

                <Menu DockPanel.Dock="Top" Height="20">
                    <MenuItem Width="20" Height="20" x:Name="MenuItem_AddNewQuality">
                        <MenuItem.Resources>
                            <Style TargetType="MenuItem">
                                <EventSetter Event="Click" Handler="MenuItem_AddNewQuality_Click"></EventSetter>
                            </Style>
                        </MenuItem.Resources>
                        <MenuItem.Background>
                            <ImageBrush ImageSource="icons8-add-50.png"/>
                        </MenuItem.Background>
                        <ItemsControl.ItemTemplateSelector>
                            <local:DataTemplateSelector_MenuItem_AddNewQuality_SelectType>
                                <local:DataTemplateSelector_MenuItem_AddNewQuality_SelectType.DataTemplate_Enabled>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name}"></TextBlock>
                                    </DataTemplate>
                                </local:DataTemplateSelector_MenuItem_AddNewQuality_SelectType.DataTemplate_Enabled>
                                <local:DataTemplateSelector_MenuItem_AddNewQuality_SelectType.DataTemplate_Disenabled>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name}" IsEnabled="False"></TextBlock>
                                    </DataTemplate>
                                </local:DataTemplateSelector_MenuItem_AddNewQuality_SelectType.DataTemplate_Disenabled>
                            </local:DataTemplateSelector_MenuItem_AddNewQuality_SelectType>
                        </ItemsControl.ItemTemplateSelector>
                    </MenuItem>
                    <MenuItem Width="20" Click="Button_RemoveSelectedQuality_Click" IsEnabled="{Binding HasItemSelectedandSelectedItemHasQuality, ElementName=window, Mode=OneWay}" Height="20">
                        <MenuItem.Background>
                            <ImageBrush ImageSource="icons8-delete-bin-50.png"/>
                        </MenuItem.Background>
                    </MenuItem>
                </Menu>

但是,MenuItem_AddNewQuality_Click()已执行两次。 然后我试图删除

<EventSetter Event="Click" Handler="MenuItem_AddNewQuality_Click"></EventSetter>

但是这次没有任何执行。 那么,有人可以告诉我这是怎么回事吗?

评论
  • 念念不忘
    念念不忘 回复

    如果您在事件处理程序中设置了该事件,那么该事件将被处理一次:

    private void MenuItem_AddNewQuality_Click(object sender, RoutedEventArgs e)
    {
        //....
        e.Handled = true;
    }