0

I'm trying to call a method in MainWindow.xaml from MainWindow.xaml.cs. I can't find anywhere how will that work.

Let's say for example i have KeyBinding in my xaml code and i want when CTRL+E is pressed the MethodIWantToCall to be called.

<Page x:Class="Example"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Example"
      mc:Ignorable="d">
<Page.InputBindings>
        <KeyBinding Command="{Binding MethodIWantToCall}" Gesture="Ctrl+E"/>
</Page.InputBindings>
</Page>
public partial class Example : Page
{
    public Example()
    {
        InitializeComponent();
    }

    public void MethodIWantToCall()
    {
        //Body
    }
}

Of course when i run the code and press Ctrl+E nothing will happen. If i put the Method in other class it will work, but in my situation i need that to be this Page which the xaml is behind.

Poisy
  • 3
  • 3
  • Why you do not override `OnKeyDown` or `OnKeyUp` in `class Example`, otherwise have a look at [Create Key binding in WPF](https://stackoverflow.com/questions/19697106/create-key-binding-in-wpf). – Hamid Reza Mohammadi Apr 28 '20 at 18:10
  • What method are you referring to? `MethodIWantToCall`? You cannot call this one from XAML without using a command. – mm8 Apr 29 '20 at 13:06

1 Answers1

0

Commands are a little more than just methods, they are actually classes. Specifically, they are an instance of any class that implements the ICommand interface. A "command" class has an Execute method, which actually does the action, as well as an optional CanExecute to determine if the command is currently able to be executed.

Here's the WPF documentation on commands: Commanding Overview.

This mostly focuses on RelayCommand which is a WPF implementation of commands that functions similarly to RoutedEvents. An alternative is to use a "RelayCommand" (a.k.a. "DelegateCommand"). RelayCommand isn't actually part of any standard .NET library, but it's a simple, commonly used class. You can find an implimentation of it in this answer.

Here is a question that discusses when/why one might use RoutedCommand over RelayCommand or vice-versa: WPF ICommand vs RoutedCommand. I've personally used both at different times.

To sum up: you need to define a custom command in Example which, when executed, will call MethodIWantToCall. The implementation will differ depending on whether you choose to use RelayCommand or RoutedCommand.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • You did great job writing this but did not answer my question. I'm asking if there is a way to call the method which is in procedural code in my xaml, for example with binding, its easy if the method wasn't in the Window or Page class, but in my case itsn't suppose to be – Poisy Apr 29 '20 at 09:06
  • @Poisy I'm not sure I understand. What do you mean by "procedural code"? From your example, it looks like you have a method in your [code-behind](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/code-behind-and-xaml-in-wpf) for the `Page` class `Example`, and you want to run it when `Ctrl+E` is pressed. Is that not correct? – Keith Stein Apr 29 '20 at 14:10