简体   繁体   English

如何在 wpf 中为菜单项添加点击事件?

[英]How to add click event for menu item in wpf?

Inside a button context menu in MainWindow.xaml在 MainWindow.xaml 的按钮上下文菜单中

<Button.ContextMenu>
                    <ContextMenu x:Name="options"
                                 Opened="OptionsMenu_Opened">

                        <MenuItem Header="Add path"
                                  Click="AddPath"
                                  InputGestureText="Ctrl+P"/>
                        
                        <MenuItem Header="Save logs"
                                  Click="SaveLogs"
                                  InputGestureText="Ctrl+S"/>

                        <MenuItem Header="Clear logs"
                                  Click="ClearLogs"
                                  InputGestureText="Ctrl+D"/>

                        <Separator/>

                        <MenuItem x:Name="allow_edit"
                                  Header="Allow editing"
                                  Click="Allow_edit_Click"/>

                        <MenuItem x:Name="topmost_switcher"
                                  Header="Window top-most"
                                  Click="Switch_Window_Topmost"/>

                        <Separator/>

                        <MenuItem x:Name="filter_setter"
                                  Header="Filter"
                                  Click="Filter_setter_Click"/>

                        <MenuItem x:Name="include_subdir"
                                  Header="Include subdirectories"
                                  Click="Include_subdir_Click"/>

                        <Separator/>

                        <MenuItem Header="View source code"
                                  Click="View_source_code_Click"/>
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>

I added the "Add path" part :我添加了“添加路径”部分:

<MenuItem Header="Add path"
                                      Click="AddPath"
                                      InputGestureText="Ctrl+P"/>

but when i try to compile the project i get error because MainWindow does not contains AddPath :但是当我尝试编译项目时出现错误,因为 MainWindow 不包含 AddPath :

'MainWindow' does not contain a definition for 'AddPath' and no accessible extension method 'AddPath' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?) “MainWindow”不包含“AddPath”的定义,并且找不到接受“MainWindow”类型的第一个参数的可访问扩展方法“AddPath”(您是否缺少 using 指令或程序集引用?)

When looking in the MainWindow.xaml.cs i see events for the other menu items for example the SaveLogs :在 MainWindow.xaml.cs 中查看时,我看到了其他菜单项的事件,例如 SaveLogs :

private void SaveLogs(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog()
                {
                    Filter = "TXT|*.txt",
                    FileName = $"watcher_{DateTime.Now:yyyyMMddHHmmss}"
                };
                if (sfd.ShowDialog() == true)
                {
                    string path = sfd.FileName;
                    string content = new TextRange(
                    richTextBox_main.Document.ContentStart,
                    richTextBox_main.Document.ContentEnd).Text;

                    File.WriteAllText(path, content);
                    PrintMsg($"saved log to \"{path}\"");
                }
            }
            catch (Exception ex)
            {
                PrintErr(ex);
            }
        }

How do i create the same event type for the AddPath ?如何为 AddPath 创建相同的事件类型? How to fix this error ?如何解决此错误?

In WPF world, you really should consider MVVM approach instead of code behind.在 WPF 世界中,您确实应该考虑 MVVM 方法而不是代码背后。 In MVVM approach, you will use command binding, not click event.在 MVVM 方法中,您将使用命令绑定,而不是单击事件。

For example:例如:

 <MenuItem Header="Add path" Command="{Binding AddPathCommand}" />

Of course this might be some substantial changes required if you do not usually have view model for the view.当然,如果您通常没有视图的视图模型,这可能需要进行一些重大更改。 But most of the stuff are already written in some mvvm framework like MvvmLight, Prism etc.但是大部分东西已经写在一些 mvvm 框架中,比如 MvvmLight、Prism 等。

I strongly suggest you go thru MVVM method using command binding.我强烈建议您使用命令绑定通过 MVVM 方法。 https://www.c-sharpcorner.com/UploadFile/6f0898/learn-simple-mvvm-and-command-bindings-in-15-mins/ https://www.c-sharpcorner.com/UploadFile/6f0898/learn-simple-mvvm-and-command-bindings-in-15-mins/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM