Home > Mobile >  How to add click event for menu item in wpf?
How to add click event for menu item in wpf?

Time:06-02

Inside a button context menu in 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' 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?)

When looking in the MainWindow.xaml.cs i see events for the other menu items for example the 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 ? How to fix this error ?

CodePudding user response:

In WPF world, you really should consider MVVM approach instead of code behind. In MVVM approach, you will use command binding, not click event.

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.

I strongly suggest you go thru MVVM method using command binding. https://www.c-sharpcorner.com/UploadFile/6f0898/learn-simple-mvvm-and-command-bindings-in-15-mins/

  • Related