How do I call a call method from XAML in WPF?
You Can create RelayCommand inheriting ICommand, and then create property of ICommand and assign relay command to that property and call the method.
While XAML is used to build user interfaces in WPF, C# is used as the code-behind languages in WPF. While Windows and their controls are created in XAML at design-time, they can also be created at runtime using C# language. C# is also used to write all events programming and business logic.
Forms application, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.
Code-behind is a term used to describe the code that is joined with markup-defined objects, when a XAML page is markup-compiled. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.
The typical way this is handled is by wrapping your method into an ICommand, and using the Commanding infrastructure in WPF.
I blogged about Commanding, showing some of the advantages of this approach, especially when you use something like the RelayCommand implementation in Josh Smith's MVVM article.
Except the commands there is another way that allows you to call a method directly from XAML. It is not commonly used but the option is still there.
The method must have one of the two types of signatures
To make it working, you have to include references and namespaces of the Microsoft.Expression.Interactions
and System.Windows.Interactivity
into your project. Easiest way is to install a nuget. In the example below the namespaces are defined as xmlns:i
and xmlns:ei
.
<Window x:Class="Interactivity.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:local="clr-namespace:Interactivity"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Run" IsEnabled="{Binding CanRun}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="VoidParameterlessMethod" TargetObject="{Binding}" />
<ei:CallMethodAction MethodName="EventHandlerLikeMethod" TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public void VoidParameterlessMethod()
=> Console.WriteLine("VoidParameterlessMethod called");
public void EventHandlerLikeMethod(object o, EventArgs e)
=> Console.WriteLine("EventHandlerLikeMethod called");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With